simple problem trying to specify a unique attribute with XML schema

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • smachin1000@gmail.com

    simple problem trying to specify a unique attribute with XML schema

    Hi All,

    In the sample schema & document below, I'd like the attribute "name" to
    be unique for all function elements under function_list. The tools I'm
    using (XML Spy and xmllint) all validate the sample document and do not
    pick up that the name is not unique.
    Any clues as to what could be going wrong?

    Thanks.

    -----------------------------------------------------------------------------------------------------------------------------
    function_list.x sd:
    <?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="function_ list">
    <xs:complexType >
    <xs:sequence maxOccurs="unbo unded">
    <xs:element ref="function"/>
    </xs:sequence>
    </xs:complexType>
    <xs:unique name="unique_fn _name">
    <xs:selector xpath="function _list"/>
    <xs:field xpath="@name"/>
    </xs:unique>
    </xs:element>
    <xs:element name="function" >
    <xs:complexType >
    <xs:attribute name="name" type="xs:string " use="required"/>
    <!-- want the above attribute to be unique -->
    </xs:complexType>
    </xs:element>
    </xs:schema>

    -----------------------------------------------------------------------------------------------------------------------------
    function_list.x ml:

    <?xml version="1.0" encoding="UTF-8"?>
    <function_lis t xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespace SchemaLocation= "function_list. xsd">
    <function name="fred"/>
    <function name="fred"/>
    <function name="frog"/>
    <function name="fred"/>
    </function_list>

  • George Bina

    #2
    Re: simple problem trying to specify a unique attribute with XML schema

    Hi,

    Your unique constrint does not select any nodes because you are in a
    function_list element and you select as selector all the cildren
    elements that have the name function_list and there are no such
    elements in your instance document

    <xs:selector xpath="function _list"/>

    What you want is probably

    <xs:selector xpath="function "/>

    Best Regards,
    George
    ---------------------------------------------------------------------
    George Cristian Bina
    <oXygen/XML Editor, Schema Editor and XSLT Editor/Debugger


    smachin1000@gma il.com wrote:
    Hi All,
    >
    In the sample schema & document below, I'd like the attribute "name" to
    be unique for all function elements under function_list. The tools I'm
    using (XML Spy and xmllint) all validate the sample document and do not
    pick up that the name is not unique.
    Any clues as to what could be going wrong?
    >
    Thanks.
    >
    -----------------------------------------------------------------------------------------------------------------------------
    function_list.x sd:
    <?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="function_ list">
    <xs:complexType >
    <xs:sequence maxOccurs="unbo unded">
    <xs:element ref="function"/>
    </xs:sequence>
    </xs:complexType>
    <xs:unique name="unique_fn _name">
    <xs:selector xpath="function _list"/>
    <xs:field xpath="@name"/>
    </xs:unique>
    </xs:element>
    <xs:element name="function" >
    <xs:complexType >
    <xs:attribute name="name" type="xs:string " use="required"/>
    <!-- want the above attribute to be unique -->
    </xs:complexType>
    </xs:element>
    </xs:schema>
    >
    -----------------------------------------------------------------------------------------------------------------------------
    function_list.x ml:
    >
    <?xml version="1.0" encoding="UTF-8"?>
    <function_lis t xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespace SchemaLocation= "function_list. xsd">
    <function name="fred"/>
    <function name="fred"/>
    <function name="frog"/>
    <function name="fred"/>
    </function_list>

    Comment

    • smachin1000@gmail.com

      #3
      Re: simple problem trying to specify a unique attribute with XML schema

      Thanks George,

      Your advice worked :)
      The other thing that was tripping me up is that my real world example
      (as opposed to the canonical example I posted here) was using a default
      namespace, so I had to some further tweaking to get the xpath expr. to
      work correctly.


      George Bina wrote:
      Hi,
      >
      Your unique constrint does not select any nodes because you are in a
      function_list element and you select as selector all the cildren
      elements that have the name function_list and there are no such
      elements in your instance document
      >
      <xs:selector xpath="function _list"/>
      >
      What you want is probably
      >
      <xs:selector xpath="function "/>
      >
      Best Regards,
      George
      ---------------------------------------------------------------------
      George Cristian Bina
      <oXygen/XML Editor, Schema Editor and XSLT Editor/Debugger

      >
      smachin1000@gma il.com wrote:
      Hi All,

      In the sample schema & document below, I'd like the attribute "name" to
      be unique for all function elements under function_list. The tools I'm
      using (XML Spy and xmllint) all validate the sample document and do not
      pick up that the name is not unique.
      Any clues as to what could be going wrong?

      Thanks.

      -----------------------------------------------------------------------------------------------------------------------------
      function_list.x sd:
      <?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="function_ list">
      <xs:complexType >
      <xs:sequence maxOccurs="unbo unded">
      <xs:element ref="function"/>
      </xs:sequence>
      </xs:complexType>
      <xs:unique name="unique_fn _name">
      <xs:selector xpath="function _list"/>
      <xs:field xpath="@name"/>
      </xs:unique>
      </xs:element>
      <xs:element name="function" >
      <xs:complexType >
      <xs:attribute name="name" type="xs:string " use="required"/>
      <!-- want the above attribute to be unique -->
      </xs:complexType>
      </xs:element>
      </xs:schema>

      -----------------------------------------------------------------------------------------------------------------------------
      function_list.x ml:

      <?xml version="1.0" encoding="UTF-8"?>
      <function_lis t xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespace SchemaLocation= "function_list. xsd">
      <function name="fred"/>
      <function name="fred"/>
      <function name="frog"/>
      <function name="fred"/>
      </function_list>

      Comment

      Working...