I think there might be a bug with nillable strings and padding when
padChar is the same as the nilValue. The following should result with a
nilled element when the data is all spaces but doesn't.
<xs:element name="field" type="xs:string"
dfdl:lengthKind="explicit" dfdl:length="9"
dfdl:textPadKind="padChar"
dfdl:textTrimKind="padChar"
dfdl:textStringPadCharacter="%SP;"
dfdl:textStringJustification="left"
dfdl:nilKind="literalCharacter"
dfdl:nilValue="%SP;"
nillable="true" />
However, if you want the element to not be in the infoset at all when
the data is all spaces, as opposed to a nilled element, you need a
different method. Keep in mind that something needs to parse those empty
strings if the element is missing.
A technique that seems to work well (although it is maybe a bit messy)
is something like this:
<xs:choice>
<xs:sequence dfdl:initiator="%SP;%SP;%SP;%SP;%SP;%SP;%SP;%SP;%SP;" />
<xs:element name="field" ... />
</xs:choice>
So in this case we first try to parse 9 spaces via an empty sequence
with an initiator. If that fails, then we try to parse those 9
characters as "field". So if 9 spaces were found then the field element
will not be in the infoset. Note that field should have minOccurs="1"
now--it's not optional since the optionality is handled by the sequence.
This is a little messy, so I'd recommend defining some formats to make
it more clear, e.g.:
<xs:annotation>
<xs:appinfo source="http://www.ogf.org/dfdl/">
<dfdl:defineFormat name="empty9">
<dfdl:format initiator="%SP;%SP;%SP;%SP;%SP;%SP;%SP;%SP;%SP;" />
</dfdl:defineFormat>
</xs:appinfo>
</xs:annotation>
<xs:choice>
<xs:sequence dfdl:ref="empty9" />
<xs:element name="field" ... />
</xs:choice>
On 2/20/20 1:03 PM, Patrick Grandjean wrote:
> Hi,
>
> I am parsing a text format and some optional elements are encoded as empty
> strings or strings with space characters only. How to have these elements
> omitted in the output XML?
>
> The element is declared as:
>
> <xs:element name="field1" type="xs:string" minOccurs="0" dfdl:length="9"
> dfdl:lengthKind="explicit" />
>
> Thanks to the properties textStringJustification="center" and
> textTrimKind="padChar", the parsed string is trimmed and the output XML looks
> like:
>
> <field1></field1>
>
> I have tried specifying properties emptyValueDelimiterPolicy, nilKind,
> nilValueDelimiterPolicy and nilValue but can't find a combination to have
> this
> element removed in the output XML.
>
> Is it possible? If yes, could you please show me how?
>
> Thanks,
> Patrick.
>