Sunday, December 11, 2011

Oracle nXSD in action: parsing EDI to XML

Schema level options:

Element level options:

lots of valuable examples here

I have a EDI document, made by several fixed length segments.
Each segment starts with a fixed label.
Each segment contains multiple fixed length fields.
Segment can appear in groups (sequence of many segments).
Groups can appear multiple times.

Lesson learned:

* since there is no fixed sequence of segments, you should use this construct
xsd:choice minOccurs="0" maxOccurs="1000"
xsd:element name="ACME0001" type="tns:ACME0001_Type" nxsd:startsWith="ACME0001"

xsd:element name="ACME0002" type="tns:ACME0002_Type" nxsd:startsWith="ACME0002"

(using maxOccurs="unbounded" sends the nXSD parser into an infinite loop)

* segments who are not needed, should still be parsed, using a generic element:

xsd:element name="ACME0001" type="tns:Any1064Block_Type" minOccurs="0" maxOccurs="10" nxsd:startsWith="ACME0001 "

where Any1064Block_Type is:

<xsd:complexType name="Any1064Block_Type">
        <xsd:element name="DATA" type="xsd:string" nxsd:style="fixedLength" nxsd:length="1054"/>

where 1054 = 1064 (length of the segment) - length of the "startsWith" element (ACME0001 )

* if the data you are interested in are in the middle of a segment, you can "skip" the initial characters:

xsd:element name="PONbr" type="xsd:string" minOccurs="1" nxsd:style="fixedLength" nxsd:length="35" nxsd:skip="136"

* you should always consume each segment to its end, maybe using a "FLUFF" element just for the sake of telling the parser to skip data:

xsd:element name="FLUFF2" type="xsd:string" minOccurs="1" nxsd:style="fixedLength" nxsd:length="883"

failing to consume a segment to its exact end will stop the parser.

No comments: