Victor Engmark wrote:
How do I define that the contents of an element should be unique only in
a sub-tree of the whole XML file?
In my case, I have several documents, each containing several files. The
file names have to be unique only within each document. I.e., the
following is valid:
<doc>
<file>AAA</file>
<file>BBB</file>
</doc>
<doc>
<file>AAA</file>
</doc>
, while the following is not:
<doc>
<file>AAA</file>
<file>BBB</file>
<file>AAA</file> <!-- Matches previous file! -->
</doc>
XML schema allows for uniqueness constraints, here is an example schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element ref="doc" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="doc">
<xs:complexType>
<xs:sequence>
<xs:element name="file" type="xs:string" maxOccurs="unbounded" />
</xs:sequence>
</xs:complexType>
<xs:unique name="uniqueFile">
<xs:selector xpath="file" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
</xs:schema>
and an example document
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="test20040430Xsd.xml ">
<doc>
<file>AAA</file>
<file>BBB</file>
</doc>
<doc>
<file>AAA</file>
<file>AAA</file>
</doc>
</root>
where the validation will flag an error for the second <file> element in
the second <doc> element.
--
Martin Honnen
http://JavaScript.FAQTs.com/