XML_Schema: problem with <xs:unique>

Discussion in 'XML' started by Aray, Jan 5, 2007.

  1. Aray

    Aray Guest

    I googled for it for few houres, but didn't resolve it, any hints will be
    thinkfull.

    I want to use the following XSD file to guarantee <element1> is unique, but
    it doesn't work.

    The following is my XSD file and XML file:

    ----------- test.xsd ----------------
    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
    elementFormDefault="qualified">

    <xs:element name="root" type="rootType">
    <xs:unique name="PK_element1">
    <xs:selector xpath="./element1"></xs:selector>
    <xs:field xpath="."></xs:field>
    </xs:unique>
    </xs:element>

    <xs:complexType name="rootType">
    <xs:choice>
    <xs:group ref="elementGroup" maxOccurs="unbounded" minOccurs="0" />
    </xs:choice>
    </xs:complexType>

    <xs:group name="elementGroup">
    <xs:choice>
    <xs:element name="element1" type="xs:string" />
    <xs:element name="element2" type="xs:string" />
    </xs:choice>
    </xs:group>

    </xs:schema>


    ----------- test.xml ----------------
    <?xml version="1.0" encoding="UTF-8"?>

    <root xmlns="http://www.aray.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.aray.com test.xsd"
    >

    <element1>1</element1>
    <element2>1</element2>
    <element1>1</element1>
    <!--
    I expect this xml file is not valide, because the two <element1> has the
    same content.
    But it is valide
    -->
    </root>



    --
     
    Aray, Jan 5, 2007
    #1
    1. Advertising

  2. Aray wrote:
    > I googled for it for few houres, but didn't resolve it, any hints will be
    > thinkfull.
    >
    > I want to use the following XSD file to guarantee <element1> is unique, but
    > it doesn't work.
    >
    > The following is my XSD file and XML file:
    >
    > ----------- test.xsd ----------------
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
    > elementFormDefault="qualified">
    >
    > <xs:element name="root" type="rootType">
    > <xs:unique name="PK_element1">
    > <xs:selector xpath="./element1"></xs:selector>
    > <xs:field xpath="."></xs:field>
    > </xs:unique>
    > </xs:element>
    >
    > <xs:complexType name="rootType">
    > <xs:choice>
    > <xs:group ref="elementGroup" maxOccurs="unbounded" minOccurs="0" />
    > </xs:choice>
    > </xs:complexType>
    >
    > <xs:group name="elementGroup">
    > <xs:choice>
    > <xs:element name="element1" type="xs:string" />
    > <xs:element name="element2" type="xs:string" />
    > </xs:choice>
    > </xs:group>
    >
    > </xs:schema>
    >
    >
    > ----------- test.xml ----------------
    > <?xml version="1.0" encoding="UTF-8"?>
    >
    > <root xmlns="http://www.aray.com"
    > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    > xsi:schemaLocation="http://www.aray.com test.xsd"
    > >

    > <element1>1</element1>
    > <element2>1</element2>
    > <element1>1</element1>
    > <!--
    > I expect this xml file is not valide, because the two <element1> has the
    > same content.
    > But it is valide
    > -->
    > </root>


    You need to specify the namespace in the XPath expression, otherwise
    "element1" is taken to be in "no namespace", which is not the case.
    That would work if there were no namespace involved at all. Here is the
    modified start of your schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
    xmlns:ns="http://www.aray.com"
    elementFormDefault="qualified">

    <xs:element name="root" type="rootType">
    <xs:unique name="PK_element1">
    <xs:selector xpath="./ns:element1"></xs:selector>
    <xs:field xpath="."></xs:field>
    </xs:unique>
    </xs:element>
    ....

    $ xmllint --noout --schema test.xsd test.xml
    test.xml:9: element element1: Schemas validity error : Element
    '{http://www.aray.com}element1': Duplicate key-sequence ['1'].
    test.xml fails to validate
     
    Arndt Jonasson, Jan 10, 2007
    #2
    1. Advertising

  3. Aray

    Aray Guest

    Thanks a lot

    --

    "Arndt Jonasson" <> ????
    news:...
    > You need to specify the namespace in the XPath expression, otherwise
    > "element1" is taken to be in "no namespace", which is not the case.
    > That would work if there were no namespace involved at all. Here is the
    > modified start of your schema:
    >
    > <?xml version="1.0" encoding="UTF-8"?>
    > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    > xmlns="http://www.aray.com" targetNamespace="http://www.aray.com"
    > xmlns:ns="http://www.aray.com"
    > elementFormDefault="qualified">
    >
    > <xs:element name="root" type="rootType">
    > <xs:unique name="PK_element1">
    > <xs:selector xpath="./ns:element1"></xs:selector>
    > <xs:field xpath="."></xs:field>
    > </xs:unique>
    > </xs:element>
    > ...
    >
    > $ xmllint --noout --schema test.xsd test.xml
    > test.xml:9: element element1: Schemas validity error : Element
    > '{http://www.aray.com}element1': Duplicate key-sequence ['1'].
    > test.xml fails to validate
    >
     
    Aray, Jan 15, 2007
    #3
    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. Newton
    Replies:
    4
    Views:
    485
    S. Justin Gengo
    Jul 23, 2003
  2. BCC
    Replies:
    2
    Views:
    1,493
  3. Replies:
    3
    Views:
    402
  4. ToshiBoy
    Replies:
    6
    Views:
    888
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    393
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page