xerces-c schema validation

Discussion in 'XML' started by Harald Haspl, Sep 1, 2005.

  1. Harald Haspl

    Harald Haspl Guest

    Hello,

    I've appended a simple XML file and it's appropriate schema below.
    I want to validate the XML file against the schema with xerces-c.

    This example contains a section where colours are defined and a section
    where referenzes to these definitions are made.
    Values are valid only if they were defined above.

    A key constraint is defined for Definition/Color/ids, keyref to
    Data/Item/color.

    If I use an undefined color value, the parser gives me this error message:
    Error at file data.xml, line 18, char 12
    "The key for identity constraint of element 'DemoFile' is not found."

    This is not very helpful to find the actual errornous line, because this
    line number is at the end of the file.

    Is there a way to get more accurate information about which line is wrong?
    Or is it impossible due to the way xerces deals with key and keyref
    constraints (only in the scope of the element)?

    best regards,
    Harald.

    ---------8<--------- data.xml -------------------

    <?xml version="1.0"?>
    <DemoFile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="schema.xsd">
    <Definition>
    <Color id="red"/>
    <Color id="green"/>
    <Color id="blue"/>
    </Definition>

    <Data>
    <Item color="red"></Item>
    <Item color="green"></Item>
    <Item color="blue"></Item>
    <Item color="green"></Item>
    <Item color="red"></Item>
    </Data>

    </DemoFile>

    ---------8<-------end of data.xml ---------------


    ---------8<--------- schema.xsd -----------------


    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="DemoFile">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Definition">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Color" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="id" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>

    <xs:element name="Data">
    <xs:complexType>
    <xs:sequence>
    <xs:element name="Item" maxOccurs="unbounded">
    <xs:complexType>
    <xs:attribute name="color" type="xs:string"/>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:sequence>
    </xs:complexType>


    <xs:key name="TypeId">
    <xs:selector xpath="Definition/Color"/>
    <xs:field xpath="@id"/>
    </xs:key>
    <xs:keyref name="IdExists" refer="TypeId">
    <xs:selector xpath="Data/Item"/>
    <xs:field xpath="@color"/>
    </xs:keyref>
    </xs:element>
    </xs:schema>


    ---------8<-------end of schema.xsd ---------------
     
    Harald Haspl, Sep 1, 2005
    #1
    1. Advertising

  2. XSV is happy with your instance. . .

    ht
    --
    Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
    Half-time member of W3C Team
    2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
    Fax: (44) 131 650-4587, e-mail:
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]
     
    Henry S. Thompson, Sep 2, 2005
    #2
    1. Advertising

  3. Harald Haspl

    Harald Haspl Guest

    Henry S. Thompson wrote:

    > XSV is happy with your instance. . .


    The schema is correct, but I would like the parser to be more precisely in
    indicating an error in the XML-File.

    When inserting an item in the Data-section which has no reference value in
    the above section:
    <Item color="yellow"></Item>

    Xerces points me to the end of the root-element[1]. Isn't it possible to
    find out that the errornous element is at line 14?


    best regards,
    Harald.


    [1]: Error message of xerces:
    line 20, char 12
    The key for identity constraint of element 'DemoFile' is not found.
     
    Harald Haspl, Sep 2, 2005
    #3
  4. Harald Haspl writes:

    > Henry S. Thompson wrote:
    >
    >> XSV is happy with your instance. . .

    >
    > The schema is correct, but I would like the parser to be more precisely in
    > indicating an error in the XML-File.
    >
    > When inserting an item in the Data-section which has no reference value in
    > the above section:
    > <Item color="yellow"></Item>
    >
    > Xerces points me to the end of the root-element[1]. Isn't it possible to
    > find out that the errornous element is at line 14?


    XSV does give the error message you want [1], but note that Xerces is not
    being unreasonable, the error isn't detected untell then end of the
    scope of the keyref.

    ht

    [1]
    <invalid char="9" code="cvc-identity-constraint.2.3.2" line="13"
    resource="file:///.../data1.xml">no key in {None}:TypeId
    for yellow</invalid>
    --
    Henry S. Thompson, HCRC Language Technology Group, University of Edinburgh
    Half-time member of W3C Team
    2 Buccleuch Place, Edinburgh EH8 9LW, SCOTLAND -- (44) 131 650-4440
    Fax: (44) 131 650-4587, e-mail:
    URL: http://www.ltg.ed.ac.uk/~ht/
    [mail really from me _always_ has this .sig -- mail without it is forged spam]
     
    Henry S. Thompson, Sep 5, 2005
    #4
  5. Harald Haspl

    Harald Haspl Guest

    On 2005-09-05, Henry S. Thompson <> wrote:
    > Harald Haspl writes:
    >> Isn't it possible to find out that the errornous element is at line 14?

    > XSV does give the error message you want [1], but note that Xerces is not
    > being unreasonable, the error isn't detected untell then end of the
    > scope of the keyref.


    Thanks for your reply, finally I found what I need:
    If parsing of an XML file fails, I can check it with xmllint afterwards
    and get more detailed indication where the error is located.

    greetings,
    Harald.
     
    Harald Haspl, Sep 8, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ole Hedegaard

    Xerces schema validation

    Ole Hedegaard, Nov 25, 2003, in forum: Java
    Replies:
    2
    Views:
    977
    Esmond Pitt
    Nov 26, 2003
  2. Markus
    Replies:
    1
    Views:
    1,675
    Markus
    Nov 23, 2005
  3. Ole Hedegaard

    Xerces schema validation

    Ole Hedegaard, Nov 25, 2003, in forum: XML
    Replies:
    1
    Views:
    615
    Florian Deissenboeck
    Nov 27, 2003
  4. Olaf Meyer
    Replies:
    2
    Views:
    720
    Olaf Meyer
    Jan 15, 2004
  5. Grand Apeiron
    Replies:
    7
    Views:
    1,803
    Peter Flynn
    Jan 20, 2004
Loading...

Share This Page