Unique constraint in XML Schema

前端 未结 2 796
故里飘歌
故里飘歌 2020-12-16 13:46

Let\'s say I have following XML file:


   a1
   a2
   2010

        
相关标签:
2条回答
  • 2020-12-16 14:18

    The selector XPath selects the nodes that must be unique (in that case, it should select the author nodes).

    The field XPath selects what "makes them unique" (in that case, using . will cause their typed value, in that case the text between the tags, treated as a string, to be used).

    The document

    <?xml version="1.0" encoding="UTF-8"?>
    <authors>
      <author>a1</author>
      <author>a2</author>
      <lastmodified>2010-01-01</lastmodified>
    </authors>
    

    should be valid against the following schema:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="authors">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="author" maxOccurs="unbounded" type="xs:string"/>
            <xs:element name="lastmodified" type="xs:date" minOccurs="0"/>
          </xs:sequence>
        </xs:complexType>
        <xs:unique name="uniqueAuthor">
          <xs:selector xpath="author"/>
          <xs:field xpath="."/>
        </xs:unique>
      </xs:element>
    </xs:schema>
    

    while this one should not:

    <?xml version="1.0" encoding="UTF-8"?>
    <authors>
      <author>a1</author>
      <author>a1</author>
      <lastmodified>2010-01-01</lastmodified>
    </authors>
    
    0 讨论(0)
  • 2020-12-16 14:21

    You could use type="xs:ID" on the author element. There is also type IDREF for referring to an ID.

    0 讨论(0)
提交回复
热议问题