Ignore order of elements using xs:extension

前端 未结 3 1376
粉色の甜心
粉色の甜心 2021-02-19 04:33

How can I design my xsd to ignore the sequence of elements?

   



        
相关标签:
3条回答
  • 2021-02-19 04:56

    <xs:complexType name="BaseType"> 
        <xs:sequence> 
            <xs:element name="a" type="xs:string" /> 
        </xs:sequence> 
    </xs:complexType> 
    
    <xs:complexType name="ExtendedType"> 
        <xs:complexContent> 
            <xs:extension base="t:BaseType"> 
                <xs:sequence> 
                    <xs:element name="b" type="xs:string" /> 
                </xs:sequence> 
            </xs:extension> 
        </xs:complexContent> 
    </xs:complexType> 
    
    <xs:element name="root" type="t:ExtendedType"></xs:element> 
    

    0 讨论(0)
  • 2021-02-19 05:14

    MAJOR EDIT Originally I missed the requirement that you need to use xsd:extension. Note that xsd:extension works as if there was xsd:sequence with contents of the base type followed by contents of the extended type. As XML Schema Primer puts it:

    When a complex type is derived by extension, its effective content model is the content model of the base type plus the content model specified in the type derivation. Furthermore, the two content models are treated as two children of a sequential group.

    Therefore, it seems that the only way to make this work is to have an empty base type and store the whole alternative in the extended type, or vice versa (everything in the base and an empty extension). Like this:

    <xsd:complexType name="ExtendedType">
       <xsd:complexContent>
          <xsd:extension base="BaseType">
             <xsd:choice>
                <xsd:sequence>
                   <xsd:element name="a" type="xsd:string"/>
                   <xsd:element name="b" type="xsd:string"/>
                </xsd:sequence>
                <xsd:sequence>
                   <xsd:element name="b" type="xsd:string"/>
                   <xsd:element name="a" type="xsd:string"/>
                </xsd:sequence>
             </xsd:choice>
          </xsd:extension>
       </xsd:complexContent>
    </xsd:complexType>
    
    <xsd:complexType name="BaseType"/>
    
    <xsd:element name="root" type="ExtendedType"/>
    
    0 讨论(0)
  • 2021-02-19 05:17

    Here is the explanation of the restriction: http://marc.info/?l=xerces-j-user&m=118712527901786&w=4

    The only extension you can make to complex type with an "all" model is to add some attributes. You can't extend with new elements.

    In my case I've done something like that:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema targetNamespace="http://www.example.com/test"
               xmlns:xs="http://www.w3.org/2001/XMLSchema"
               xmlns:t="http://www.example.com/test" >
    
        <xs:complexType name="BaseType">
            <xs:all>
                <xs:element name="a" type="xs:string" />
            </xs:all>
        </xs:complexType>
    
        <xs:complexType name="ExtendedType">
            <xs:all>
                <xs:element name="a" type="xs:string" /> <!-- copy/paste from BaseType -->
                <xs:element name="b" type="xs:string" />
            </xs:all>
        </xs:complexType>
    
        <xs:element name="root" type="t:ExtendedType"></xs:element>
    </xs:schema>
    
    0 讨论(0)
提交回复
热议问题