You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@daffodil.apache.org by Larry Barber <la...@nteligen.com> on 2024/03/27 17:14:57 UTC

Parsing optional line prefix

I'm parsing at text file which allows an optional ':' at the beginning of lines.
The following code works at capturing this character:
                                                    <!-- optional prefix ":" -->
                                                    <xs:element name="prefix" type="xs:string" maxOccurs="1" minOccurs="0"
                                                                dfdl:lengthKind="pattern" dfdl:lengthPattern=":" dfdl:encoding="ISO-8859-1" >
                                                        <xs:annotation>
                                                            <xs:appinfo source=http://www.ogf.org/dfdl/>
                                                                <dfdl:discriminator test="{ (dfdl:valueLength(., 'bytes') eq 1) }" />
                                                            </xs:appinfo>
                                                        </xs:annotation>
                                                    </xs:element>
I wanted to avoid using REGEX and changed the code as follows:
                                                    <!-- optional prefix ":" -->
                                                    <xs:element name="prefix" type="xs:string" maxOccurs="1" minOccurs="0"
                                                                dfdl:lengthKind="explicit" dfdl:length="1" >
                                                        <xs:annotation>
                                                            <xs:appinfo source=http://www.ogf.org/dfdl/>
                                                                <dfdl:discriminator test="{ xs:string(.) eq ':' }" />
                                                            </xs:appinfo>
                                                        </xs:annotation>
                                                    </xs:element>
I thought that this was working initially, but now it seems to just consume the ':' character, but not put it into the infoset.
Not sure what's happening. Guess I'll just stick with the REGEX - it's only one character....

Re: Parsing optional line prefix

Posted by Steve Lawrence <sl...@apache.org>.
Both snippets work for me and end up with "<prefix>:</prefix>" in the infoset 
for lines that have a colon prefix.

Can you provide more of your schema or example data? Sounds like there is 
something else going on, maybe bad data or a schema bug somewhere else.

On 2024-03-27 01:14 PM, Larry Barber wrote:
> I’m parsing at text file which allows an optional ‘:’ at the beginning of lines.
> 
> The following code works at capturing this character:
> 
> <!-- optional prefix ":" -->
> 
> <xs:elementname="prefix"type="xs:string"maxOccurs="1"minOccurs="0"
> 
> dfdl:lengthKind="pattern"dfdl:lengthPattern=":"dfdl:encoding="ISO-8859-1">
> 
> <xs:annotation>
> 
> <xs:appinfosource=http://www.ogf.org/dfdl/ <http://www.ogf.org/dfdl/>>
> 
> <dfdl:discriminatortest="{ (dfdl:valueLength(.,'bytes')eq1)}" />
> 
> </xs:appinfo>
> 
> </xs:annotation>
> 
> </xs:element>
> 
> I wanted to avoid using REGEX and changed the code as follows:
> 
> <!-- optional prefix ":" -->
> 
> <xs:elementname="prefix"type="xs:string"maxOccurs="1"minOccurs="0"
> 
> dfdl:lengthKind="explicit"dfdl:length="1">
> 
> <xs:annotation>
> 
> <xs:appinfosource=http://www.ogf.org/dfdl/ <http://www.ogf.org/dfdl/>>
> 
> <dfdl:discriminatortest="{ xs:string(.)eq':'}" />
> 
> </xs:appinfo>
> 
> </xs:annotation>
> 
> </xs:element>
> 
> I thought that this was working initially, but now it seems to just consume the 
> ‘:’ character, but not put it into the infoset.
> 
> Not sure what’s happening. Guess I’ll just stick with the REGEX – it’s only one 
> character….
>