Semi-unique IDs in Schema

V

Victor Engmark

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

Martin Honnen

Victor said:
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.
 
V

Victor Engmark

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

I tried this, but I have obviously overlooked something, because the
file http://vengmark.home.cern.ch/vengmark/moi/examples/input.xml is
valid according to
http://vengmark.home.cern.ch/vengmark/moi/examples/moi.xsd, even though
none of the ID elements are unique. Sorry the example is verbose...
 
V

Victor Engmark

Victor said:
I tried this, but I have obviously overlooked something, because the
file http://vengmark.home.cern.ch/vengmark/moi/examples/input.xml is
valid according to
http://vengmark.home.cern.ch/vengmark/moi/examples/moi.xsd, even though
none of the ID elements are unique. Sorry the example is verbose...

Disregard the previous message; I had got the scope of the unique
element wrong. Nevertheless, XML Spy v5 is unable to generate even
simplistic valid files of the new Schema document. Does anyone know a
tool with which I can generate "good" sample documents from Schemas? It
should give at least some control over the values entered into fields,
and shouldn't use several hundred MBs to generate a few MBs of XML...
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top