Semi-unique IDs in Schema

Discussion in 'XML' started by Victor Engmark, Apr 30, 2004.

  1. 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
    Victor Engmark, Apr 30, 2004
    #1
    1. Advertising

  2. 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/
    Martin Honnen, Apr 30, 2004
    #2
    1. Advertising

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

    --
    Victor
    Victor Engmark, May 3, 2004
    #3
  4. Victor Engmark wrote:

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

    --
    Victor
    Victor Engmark, May 3, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Tolman
    Replies:
    0
    Views:
    2,957
    Jeff Tolman
    Nov 4, 2003
  2. Darrel

    manually creating unique IDs

    Darrel, Oct 30, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    413
    Guadala Harry
    Oct 31, 2004
  3. SouthSpawn
    Replies:
    1
    Views:
    3,121
    Scott Allen
    Apr 22, 2005
  4. Replies:
    2
    Views:
    693
    Beat Bolli
    Jun 21, 2005
  5. James Stroud

    py2app semi-standalone semi-works

    James Stroud, Oct 4, 2006, in forum: Python
    Replies:
    2
    Views:
    683
    James Stroud
    Oct 4, 2006
Loading...

Share This Page