xerces-c schema validation

H

Harald Haspl

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 ---------------
 
H

Henry S. Thompson

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: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 
H

Harald Haspl

Henry said:
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.
 
H

Henry S. Thompson

Harald said:
Henry said:
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: (e-mail address removed)
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
 
H

Harald Haspl

Harald said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top