XSD allow attribute only depending on other attribute value

前端 未结 1 1726
攒了一身酷
攒了一身酷 2021-01-20 03:07

Suppose I have an XML element, food, that can take on one of two forms:




        
相关标签:
1条回答
  • 2021-01-20 03:55

    You can do this using XSD 1.1's Conditional Type Assignment:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
               elementFormDefault="qualified"
               vc:minVersion="1.1"> 
      <xs:element name="food">
        <xs:alternative test="@kind = 'juice'" type="JuiceType"/> 
        <xs:alternative test="@kind = 'fruit'" type="FruitType"/>
      </xs:element>
      <xs:complexType name="JuiceType">
        <xs:sequence>
          <!-- ... -->
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="FruitType">
        <xs:sequence>
          <!-- ... -->
        </xs:sequence>
        <xs:attribute name="sort"/>
      </xs:complexType>
    </xs:schema>
    

    Do consider, however, an alternative design that's expressible in both XSD 1.1 and 1.0 that's generally preferable:

    <juice/>
    <fruit sort="apple"/>
    

    That is, rather than have a kind attribute, use the element name to convey kind.

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