Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Semi-unique IDs in Schema

Reply
Thread Tools

Semi-unique IDs in Schema

 
 
Victor Engmark
Guest
Posts: n/a
 
      04-30-2004
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>

--
Victor
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      04-30-2004


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 xmlnss="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 xmlnssi="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/

 
Reply With Quote
 
 
 
 
Victor Engmark
Guest
Posts: n/a
 
      05-03-2004
Martin Honnen wrote:
>
>
> 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 xmlnss="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 xmlnssi="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/vengmar...ples/input.xml is
valid according to
http://vengmark.home.cern.ch/vengmar...amples/moi.xsd, even though
none of the ID elements are unique. Sorry the example is verbose...

--
Victor
 
Reply With Quote
 
Victor Engmark
Guest
Posts: n/a
 
      05-03-2004
Victor Engmark wrote:

> I tried this, but I have obviously overlooked something, because the
> file http://vengmark.home.cern.ch/vengmar...ples/input.xml is
> valid according to
> http://vengmark.home.cern.ch/vengmar...amples/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...

--
Victor
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with schema-validation and property "http://apache.org/xml/properties/schema/external-schemaLocation" Markus Java 1 11-23-2005 02:41 PM
after transfer of data from MS-outlook(mail ids) to application,mail ids are consisting of strange characters vamsikrishna_b@coolgoose.com Python 2 06-21-2005 12:41 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
Re: Convert DB2 schema to XML Schema Klaus Johannes Rusch XML 0 08-06-2003 11:23 AM
Re: Allowing Duplicate IDs - XML Schema GB XML 0 06-24-2003 01:01 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57