Xsd and inheritance

前端 未结 4 1737
野性不改
野性不改 2020-12-28 14:27

I have an xsd like this

  
          
              
                

        
相关标签:
4条回答
  • 2020-12-28 14:55

    Simply add an <xsd:sequence> with the required elements:

    <xsd:complexType name="B">  
        <xsd:complexContent>
            <xsd:extension base="A">
               <xsd:sequence>
                  <xsd:element name="Hours">
                  ...
                  </xsd:element>
               </xsd:sequence>
            </xsd:extension>
        </xsd:complexContent>
    </xsd:complexType>
    
    0 讨论(0)
  • 2020-12-28 15:04

    You'll need to create a type for options, which contains hours etc, and then add options instead of hours in csgero's answer.

    0 讨论(0)
  • 2020-12-28 15:12

    Here's the schema I came up with:

    <?xml version="1.0" encoding="utf-8"?>
    <xs:schema id="inheritance"
        targetNamespace="http://test.com"
        elementFormDefault="qualified"
        xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:test="http://test.com"
    >
        <xs:element name="Time">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="First" type="test:A" />
                    <xs:element name="Second" type="test:B" />
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    
        <xs:complexType name="shortOptions">
            <xs:sequence>
                <xs:element name="Day" />
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="longOptions">
            <xs:complexContent>
                <xs:extension base="test:shortOptions">
                    <xs:sequence>
                        <xs:element name="Week" />
                    </xs:sequence>
                </xs:extension>
            </xs:complexContent>
        </xs:complexType>
    
        <xs:complexType name="A">
            <xs:sequence>
                <xs:element name="options" type="test:shortOptions" />
            </xs:sequence>
        </xs:complexType>
    
        <xs:complexType name="B">
            <xs:sequence>
                <xs:element name="options" type="test:longOptions" />
            </xs:sequence>
        </xs:complexType>
    
    </xs:schema>
    

    Which seems to fit this xml:

    <?xml version="1.0" encoding="utf-8" ?>
    <Time xmlns="http://test.com">
        <First>
            <options>
                <Day>Today</Day>
            </options>
        </First>
        <Second>
            <options>
                <Day>Tomorrow</Day>
                <Week>This Week</Week>
            </options>
        </Second>
    </Time>
    
    0 讨论(0)
  • 2020-12-28 15:12

    You will need to define options as a complex type of it's own, then use extension on that to create a new options complex type and use substitution instead of extension.

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