Namespaces for UN/EDIFACT in Smooks
It's done. My pull request accepted and merged into the Smooks github repository. Inside this monster commit (it was actually 10 commits squashed into 1 - p.s. git is awesome) we have now a support for the namespace declaration inside the SAX events generated by UN/EDIFACT parser. In this blog post I will explain how can you use it.
Firs change we have is that we created a new version of the EDI Mapping schema http://www.milyn.org/schema/edi-message-mapping-1.5.xsd so we don't break a compatibility to previous versions. Main changes so far are addition of the namespace attribute to the top element of mapping, so if before you were using
<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.5.xsd">
<medi:description name="Test Message" version="1.0" />
Now you can declare a namespace of the elements that will be generated by UN/EDIFACT parser using namespace attribute:
<?xml version="1.0" encoding="UTF-8"?>
<medi:edimap xmlns:medi="http://www.milyn.org/schema/edi-message-mapping-1.5.xsd" namespace="urn:my:namespace">
<medi:description name="Test Message" version="1.0" />
Please note this attribute is optional, if you omit it EDIFACT parser will not set any namespace into SAX events.
Second change is now ECT (tool that generate Smooks UN/EDIFACT mapping files based on the UN/EDIFACT directories) will automatically generate namespace attributes on the mapping files. It will use following rules:
- Directory namespaces will have following format http://www.milyn.org/schema/edi/un/[RELEASE]/[STANDARD].xsd for example for CUSCAR version d99a it will generate namespace http://www.milyn.org/schema/edi/un/d99a/cuscar.xsd
- Common definitions files (reused by all definition files within one release) will have following namespace http://www.milyn.org/schema/edi/un/[RELEASE]/common.xsd for example for d99a it will be http://www.milyn.org/schema/edi/un/d99a/common.xsd
- We also have a namespace for the EDI envelope (UNH, UNZ,... segments) namespace is fixed (for the envelope version), right now it's http://www.milyn.org/schema/edi/unedifact-header-41.xsd
And off course with all these changes UN/EDIFACT parser will now produce a namesapce aware SAX events (including calls to startPrefixMapping/endPrefixMapping and xmlns attributes), so XML produced by the EDI Parser will looks like following:
<?xml version="1.0" encoding="UTF-8"?>
<h:unEdifact xmlns:h="http://www.milyn.org/schema/edi/un/header-4.1.xsd">
<h:UNB>
<h:syntaxIdentifier>
<h:id>UNOA</h:id>
<h:versionNum>2</h:versionNum>
</h:syntaxIdentifier>
<h:sender>
<h:id>SENDER</h:id>
</h:UNB>
<h:interchangeMessage>
<h:UNH>
<h:messageRefNum>163477</h:messageRefNum>
<h:messageIdentifier>
<h:id>CUSCAR</h:id>
<h:versionNum>D</h:versionNum>
<h:releaseNum>99A</h:releaseNum>
<h:controllingAgencyCode>UN</h:controllingAgencyCode>
<h:associationAssignedCode>SCPRBL</h:associationAssignedCode>
</h:messageIdentifier>
</h:UNH>
<ns1:CUSCAR xmlns:ns1="http://www.milyn.org/schema/edi/un/d99a/cuscar.xsd">
<ns1:Beginning_of_message>
<ns2:DOCUMENT_MESSAGE_NAME
xmlns:ns2="http://www.milyn.org/schema/edi/un/d99a/common.xsd" />
<ns2:DOCUMENT_MESSAGE_IDENTIFICATION
xmlns:ns2="http://www.milyn.org/schema/edi/un/d99a/common.xsd">
<ns2:Document_message_number>MOL-EU2-HFA-012W-XXXX8896514-01
</ns2:Document_message_number>
</ns2:DOCUMENT_MESSAGE_IDENTIFICATION>
You can find a full XML file here.
Please note that header elements (UNB, UNH, etc) are declared in envelope namespace, CUSCAR elements (CUSCAR, Beginning_of_message, etc) are declared in CUSCAR specific namespace and common elements (DOCUMENT_MESSAGE_NAME, DOCUMENT_MESSAGE_IDENTIFICATION) are declared in common namespace.
So, to summarize the changes, on the first sight XML produced with namespace support could be seen as less readable. It is so, indeed, however namespace support is a first major step towards XML Schema support for generated UN/EDIFACT XML and in future a must-have for X12 or HL7 support we are going to implement in Smooks. Next step would be to simplify generated XML by using shorter element names as well automatic generation of XML Schema documents based on the namespace aware EDI mapping files. Stay tuned or follow me at @zubairov or @smooksframework.