XML Schema: unique constraint + target namespace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tthunder@gmx.de

    XML Schema: unique constraint + target namespace

    Hi @all,

    Please check the following XML file and XML schema definition below
    first:

    -------
    XML File (full):
    -------

    <?xml version="1.0" encoding="UTF-8"?>
    <Root xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespace SchemaLocation= "c:\example.xsd ">
    <Sect>
    <no>6</no>
    </Sect>
    <Sect>
    <no>6</no>
    </Sect>
    </Root>

    -------
    XML Schema File (full):
    -------

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied">
    <xs:element name="Root">
    <xs:complexType >
    <xs:sequence>
    <xs:element ref="Sect" minOccurs="0" maxOccurs="unbo unded"/>
    </xs:sequence>
    </xs:complexType>
    <xs:unique name="dateAndPr odNumKey">
    <xs:selector xpath="Sect"/>
    <xs:field xpath="no"/>
    </xs:unique>
    </xs:element>
    <xs:element name="Sect">
    <xs:complexType >
    <xs:sequence>
    <xs:element ref="no"/>
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="no" type="xs:intege r"/>
    </xs:schema>

    -------

    This example works perfect, or in other words, the validation fails,
    because there is a voilation of the "unqiue" constraint.

    Now, I change the files and use the following "options":

    -------
    XML File (snippet):
    -------

    <Root xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.example.org/graph" xsi:schemaLocat ion="http://
    www.example.org/graph c:\example.xsd" >

    -------
    XML Schema File (snippet):
    -------

    <xs:schema xmlns="http://www.example.org/graph" xmlns:xs="http://
    www.w3.org/2001/XMLSchema" targetNamespace ="http://www.example.org/
    graph" elementFormDefa ult="qualified"
    attributeFormDe fault="qualifie d">

    -------

    The only thing changed is, that I am using a target namespace.

    Basically, validation still works. If I insert wrong elements, the
    validation fails. Otherwise it's ok.
    However, the unique constraint DOES NOT work any more! That means,
    that the example XML postet above is considered valid!

    I am testing about one day now... no clue, why validation does not
    work as expected.
    All validation tools I tested so far (XMLSpy, libxml2,...) consider
    the XML file valid!

    Thanks a lot in advance,
    Kirsten
  • Martin Honnen

    #2
    Re: XML Schema: unique constraint + target namespace

    tthunder@gmx.de wrote:
    <xs:unique name="dateAndPr odNumKey">
    <xs:selector xpath="Sect"/>
    <xs:field xpath="no"/>
    </xs:unique>
    </xs:element>
    <xs:schema xmlns="http://www.example.org/graph" xmlns:xs="http://
    www.w3.org/2001/XMLSchema" targetNamespace ="http://www.example.org/
    graph" elementFormDefa ult="qualified"
    attributeFormDe fault="qualifie d">
    >
    -------
    >
    The only thing changed is, that I am using a target namespace.
    >
    Basically, validation still works. If I insert wrong elements, the
    validation fails. Otherwise it's ok.
    However, the unique constraint DOES NOT work any more! That means,
    that the example XML postet above is considered valid!
    The W3C XML schema language for unique and key constraints uses (a
    subset of) the XPath 1.0 language. In that language the XPath expression
    'Sect' always selects elements with local name 'Sect' in _no_ namespace.
    However your schema after the changes has a target namespace
    http://www.example.org/graph and defines its elements in that target
    namespace while the unique constraint still applies to elements in no
    namespace.
    So you need to change those XPath expressions to

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefa ult="qualified" attributeFormDe fault="unqualif ied"
    targetNamespace ="http://www.example.org/graph"
    xmlns:tns="http ://www.example.org/graph">
    <xs:element name="Root">
    <xs:complexType >
    <xs:sequence>
    <xs:element ref="tns:Sect" minOccurs="0" maxOccurs="unbo unded"/>
    </xs:sequence>
    </xs:complexType>
    <xs:unique name="dateAndPr odNumKey">
    <xs:selector xpath="tns:Sect "/>
    <xs:field xpath="tns:no"/>
    </xs:unique>
    </xs:element>
    --

    Martin Honnen

    Comment

    Working...