469,285 Members | 2,588 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,285 developers. It's quick & easy.

xerces-c schema validation

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 ---------------
Sep 1 '05 #1
5 1783
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: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Sep 2 '05 #2
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.
Sep 2 '05 #3
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: ht@inf.ed.ac.uk
URL: http://www.ltg.ed.ac.uk/~ht/
[mail really from me _always_ has this .sig -- mail without it is forged spam]
Sep 5 '05 #4
On 2005-09-05, Henry S. Thompson <ht@inf.ed.ac.uk> 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.

Sep 8 '05 #5
I'm interesting to this topic, also because I get the same problem[1], that's to say the XercesDOMParser didn't show the right place where error is located.

Henry, how do you get the right error info [2] ?

[1]: Error message of xerces:
line 20, char 12
The key for identity constraint of element 'DemoFile' is not found.

[2]: <invalid char="9" code="cvc-identity-constraint.2.3.2" line="13"
resource="file:///.../data1.xml">no key in {None}:TypeId
for yellow</invalid>
Sep 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Bekkali Hicham | last post: by
reply views Thread by Waseem | last post: by
reply views Thread by Dale Gerdemann | last post: by
3 posts views Thread by Raphael Tagliani | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.