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