Limit number of elements with attribute via XSD?

后端 未结 1 2038
花落未央
花落未央 2021-01-25 08:39

There is a fragment of XML


    uid-1
    uid-2
    uid-3&         


        
相关标签:
1条回答
  • 2021-01-25 09:24

    You cannot express your constraint in XSD 1.0, but in XSD 1.1, you can use xs:assert to limit the itemUID elements with key attributes to 0, 2, 3 elements as follows:

      <xs:assert test="count(itemUID[@key]) = (0, 2, 3)"/>
    

    Here it is in context in a complete XSD:

    XSD 1.1

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema attributeFormDefault="unqualified" 
      elementFormDefault="qualified" 
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
      vc:minVersion="1.1">
    
      <xs:element name="items">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="itemUID" minOccurs="1" maxOccurs="unbounded">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:string">
                     <xs:attribute name="key" type="xs:boolean">
                     </xs:attribute>
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
            </xs:element>
          </xs:sequence>
          <xs:assert test="count(itemUID[@key]) = (0, 2, 3)"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    
    0 讨论(0)
提交回复
热议问题