How to make type depend on attribute value using Conditional Type Assignment

前端 未结 1 1036
暖寄归人
暖寄归人 2020-11-22 16:58

I have an XML file like this


  
    
    
  
  

        
相关标签:
1条回答
  • 2020-11-22 17:06

    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="listOfA">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="a" maxOccurs="unbounded">
              <xs:alternative test="@type = 1" type="a1Type"/>        
              <xs:alternative test="@type = 2" type="a2Type"/>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:complexType name="a1Type">
        <xs:sequence>
          <xs:element name="name"/>
          <xs:element name="surname"/>
        </xs:sequence>
      </xs:complexType>
      <xs:complexType name="a2Type">
        <xs:sequence>
          <xs:element name="name"/>
          <xs:element name="id"/>
        </xs:sequence>
      </xs:complexType>
    </xs:schema>
    
    0 讨论(0)
提交回复
热议问题