开发者

Problem validating XSD with 'choice' and 'extension' elements

I will post two examples, one that validates and one that doesn't. I am interested in learning why the second one doesn't work since they are very similar.

Example 1 (validates)

xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
  <xsd:element name="root" type="BBB"/>
    <xsd:complexType name="AAA">
      <xsd:choice maxOccurs="2">
        <xsd:element name="x" type="xsd:string"/>
        <xsd:element name="y" type="xsd:string"/>
      </xsd:choice>
    </xsd:complexType>
  <xsd:complexType name="BBB">
    <xsd:complexContent>
      <xsd:extension base="AAA"/>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>

xml:

<root xsi:noNamespaceSchemaLocation="incorrect.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
   <x>1</x>
   <y>1</y>
</root>

Example 2 (does not validate)

xsd:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >
   <xsd:element name="root" type="BBB"/>
   <xsd:complexType name="AAA">
      <xsd:choice maxOccurs="2">
         <xsd:element name="x" type="xsd:string"/>
         <xsd:element name="y" type="xsd:string"/>
      </xsd:choice>
   </xsd:complexType>
   <xsd:complexType name="BBB">
      <xsd:complexContent>
         <xsd:extension base="AAA">
            <xsd:choice>
               <xsd:element name="z" type="xsd:string"/>
            </xsd:choice>
         </xsd:extension>
      </xsd:complexContent>
   </xsd:complexType>
</xsd:schema>

xml: (the same as Example 1)

<root xsi:noNamespaceSchemaLocation="incorrect.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"开发者_运维技巧 >
   <x>1</x>
   <y>1</y>
</root>

To save you the time running the diff against the xsd's, the only difference is that in the second example complex type BBB has a <choice>

xmllint says the following about Example 2:

$ xmllint --noout --schema example2.xsd example2.xml

example2.xml:3: element y: Schemas validity error : Element 'y': This element is not expected. Expected is ( z ).

example2.xml fails to validate


Examples loosely based off of http://www.zvon.org/xxl/XMLSchemaTutorial/Output/ser_ext_patterns_st1.html


In the xml for your invalid case, you're missing the required "z" element.

<root xsi:noNamespaceSchemaLocation="incorrect.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
   <x>1</x>
   <y>1</y>
   <z>1</z>
</root>

Validates on DecisionsSoft's Validator and xmlme's validator

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜