I see a couple issues here.

First your record element is not optional but you have 
dfdl:occursCountKind="implicit" on it, suggesting you wanted minOccurs="0" on 
it, or maxOccurs="unbounded",  but you didn't put that.

Same for it02 element, which probably wants minOccurs="0".

Second, your records type has a sequence with postfix newline separator.
Your inner sequence inside the record type *also* has postfix newline separator.

This means you will have TWO newlines required at the end of your data.

If you don't have two newlines, then it's going to backtrack and, while I can't 
construct a specific reason why it ends up giving you an error on it03, I can 
imagine that could happen.

I suggest removing the postfix from the inner sequence so it is only infix 
separated, and the final newline will come from the outer sequence.

Then I suggest making it02 element optional and record element optional or 
removing the dfdl:occursCountKind="implicit" from either or both of them.

Then try again. Post your results here again.

You may want to turn on the trace feature. You get TONS of sometimes obscure 
output from trace (fixing that is on our roadmap), but often one can wade 
through it and spot where things go wrong.

If your format has many many of these different record sub-types you will 
eventually want to switch away from using initiators to a choice using the 
dfdl:choiceDispatchKey property.

But we should get what you started with working properly first.




________________________________
From: Patrick Grandjean <[email protected]>
Sent: Monday, February 3, 2020 2:23 PM
To: [email protected] <[email protected]>
Subject: Optional element with initiator

Hi!

I'm trying to create my first DFDL schema to parse a fixed column format. The 
format may change from one line to the other depending on the first character 
of the line. Some lines are optional, therefore I use minOccurs="0" and 
dlfl:occursCountKind="implicit". Here is the schema:


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema";
           xmlns:dfdl="http://www.ogf.org/dfdl/dfdl-1.0/";
           xmlns:ex="http://example.com";
           xmlns:ama="http://www.amadeus.com";
           targetNamespace="http://www.amadeus.com";
           elementFormDefault="unqualified">

    <xs:include 
schemaLocation="org/apache/daffodil/xsd/DFDLGeneralFormat.dfdl.xsd" />
    <xs:include schemaLocation="IT01.dfdl.xsd" />
    <xs:include schemaLocation="IT02.dfdl.xsd" />
    <xs:include schemaLocation="IT03.dfdl.xsd" />
    <xs:include schemaLocation="IT05.dfdl.xsd" />

    <xs:annotation>
        <xs:appinfo source="http://www.ogf.org/dfdl/";>
            <dfdl:format alignment="1"
                         alignmentUnits="bytes"
                         encoding="ASCII"
                         encodingErrorPolicy="replace"
                         escapeSchemeRef=""
                         floating="no"
                         ignoreCase="no"
                         initiatedContent="no"
                         initiator=""
                         leadingSkip="0"
                         lengthKind="implicit"
                         lengthUnits="bytes"
                         outputNewLine="%LF;"
                         representation="text"
                         separator=""
                         separatorSuppressionPolicy="never"
                         sequenceKind="ordered"
                         terminator=""
                         textBidi="no"
                         textPadKind="none"
                         textStringJustification="center"
                         textStringPadCharacter="%WSP;"
                         textTrimKind="padChar"
                         trailingSkip="0"
                         truncateSpecifiedLengthString="no"/>
        </xs:appinfo>
    </xs:annotation>

    <xs:element name="records" type="ama:Records" />

    <xs:complexType name="Records">
        <xs:sequence dfdl:separator="%NL;" dfdl:separatorPosition="postfix">
            <xs:element name="it01" minOccurs="0" maxOccurs="1" type="ama:IT01" 
dfdl:initiator="1" dfdl:occursCountKind="implicit" />
            <xs:element name="record" type="ama:Record" minOccurs="1" 
dfdl:occursCountKind="implicit" />
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Record">
        <xs:sequence dfdl:separator="%NL;" dfdl:separatorPosition="postfix">
            <xs:element name="it02" type="ama:IT02" dfdl:initiator="2" 
dfdl:occursCountKind="implicit" />
            <xs:element name="it03" minOccurs="0" type="ama:IT03" 
dfdl:initiator="3" dfdl:occursCountKind="implicit" />
            <xs:element name="it05" minOccurs="0" type="ama:IT05" 
dfdl:initiator="5" dfdl:occursCountKind="implicit" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

A file would look like this:

1......
2......
5......

Elements it01 (line starting with '1') and it02 (line starting with '2') are 
parsed successfully. Element it03 is missing, but since it is optional it is 
OK. The problem is that the Daffodil raises an error saying it has not found 
initiator '3'. How to make the initiator optional?

Patrick.

Reply via email to