XSD:如何制作元素标签取决于前面的元素?

时间:2016-02-11 03:52:03

标签: xml xsd

我想要的结构是这样的。

<root>
   ... 
   <animaltype>BIRD</animaltype>
   <birdname>eagle<birdname>
   ...
</root>

<root>
    ...
    <animaltype>LIZARD</animaltype>
    <reptilename>iguana</reptilename>
    ...
</root>

我在互联网上发现的大部分内容只允许我创建自己的鸟或蜥蜴元素,这将是另一个层次:

<lizard>
     <name>iguana</name>
</lizard>

1 个答案:

答案 0 :(得分:0)

我认为这是不可能的。

我尝试了下面的内容: -

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root">
        <xs:complexType>
            <xs:sequence>
                <xs:choice>
                    <xs:sequence>
                        <xs:element name="animaltype" type="xs:string" fixed="BIRD"/>
                        <xs:element name="birdname" type="xs:string">
                    </xs:element>
                    </xs:sequence>
                    <xs:sequence>
                        <xs:element name="animaltype" type="xs:string" fixed="LIZARD"/>
                        <xs:element name="reptilename" type="xs:string">
                     </xs:element>
                    </xs:sequence>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这似乎不起作用。似乎是验证https://www.w3.org/TR/2004/REC-xmlschema-1-20041028/#cos-nonambig,该名称不能含糊不清。

在我看来,最好的解决方案是你提到的动物和鸟类元素的不同名称,或者有更宽松的模式。

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="root" >
    <xs:complexType>
        <xs:sequence>
         <xs:element name="animaltype" type="xs:string" />
         <xs:choice>
             <xs:element name="birdname" type="xs:string"></xs:element>
             <xs:element name="reptilename" type="xs:string"></xs:element>
         </xs:choice>
         </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
相关问题