【问题标题】:XML schema on type substitution类型替换的 XML 模式
【发布时间】:2012-11-26 00:41:20
【问题描述】:

我有一个如下所示的 XML 文件,并希望使用类型替换方法创建一个 XML 模式,以便它可以验证下面的 XML 文件。但是我创建的架构是完全错误的。请告诉我如何编写架构来验证下面的 XML 文件。

详情:

  • 只储存两种动物,一种是鸟,一种是鱼。
  • 类型、名称和来源元素都是必需的
  • 对于 type:bird,可以选择存储额外的颜色元素。
  • 对于type:fish,需要存储额外的size元素

    <animals>
     <animal animalID="b-1" xsi:type="bird">
         <name>Humming Bird</name>
         <origin>Asia</origin>
         <color>Blue</color>
     </animal> 
     <animal animalID="b-2" xsi:type="bird">
         <name>Horn Bill</name>
         <origin>Asia</origin>
     </animal>
     <animal animalID="f-2" xsi:type="fish">
         <name>Whale</name>
         <origin>Europe</origin>
         <size>Large</size>
     </animal>
     <animal animalID="b-5" xsi:type="bird">
         <name>Parrot</name>
         <origin>Europe</origin>
     </animal>
    

我提出了以下架构,我认为它完全错误。

 <xsd:element name="bird" substitutionGroup="animals" 
         type="birdType"/>
 <xsd:element name="fish" substitutionGroup="animals" 
         type="fishType"/>
 <xsd:element name="animals">
<xsd:complexType>
    <xsd:sequence>
        <xsd:element name="animal" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
</xsd:element>

 <xsd:element name="animal">
<xsd:complexType>
    <xsd:sequence>
        <xsd:element ref="bird" maxOccurs="unbounded"/>
    </xsd:sequence>
</xsd:complexType>
</xsd:element>

【问题讨论】:

    标签: xml schema


    【解决方案1】:

    我不是生物学家,但如果我是的话,我会和你的分类学争吵...

    如果您使用 xsi:type 来区分这两种类型,则架构需要包含名为“bird”和“fish”的全局复杂类型定义。您可以通过从某些基本类型(例如“生物”)扩展来获得这两者(因为我们在这里没有做真正的生物学......)。类型生物包含两个通用元素名称和来源,两个扩展分别包含可选元素颜色和大小。动物元素被定义为“生物”类型。

    【讨论】:

      【解决方案2】:
      <?xml version="1.0"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
      <xsd:complexType name="animalType">
          <xsd:sequence>
              <xsd:element name="name" type="xsd:string" minOccurs="1" maxOccurs="1"/>
              <xsd:element name="origin" type="xsd:string" minOccurs="1" maxOccurs="1"/>
          </xsd:sequence>
      </xsd:complexType>
      <xsd:complexType name="birdType">
          <xsd:complexContent>
              <xsd:extension base="animalType">
                  <xsd:sequence>
                      <xsd:element name="color" type="xsd:string" minOccurs="0"/>
                  </xsd:sequence>
              </xsd:extension>
          </xsd:complexContent>
      </xsd:complexType>
      <xsd:complexType name="fishType">
          <xsd:complexContent>
              <xsd:extension base="animalType">
                  <xsd:sequence>
                      <xsd:element name="size" type="xsd:string" minOccurs="1"/>
                  </xsd:sequence>
              </xsd:extension>
          </xsd:complexContent>
      </xsd:complexType>
      <xsd:element name="animals">
          <xsd:complexType>
              <xsd:sequence>
                  <xsd:element name="animal" type="animalType" maxOccurs="unbounded"/>
              </xsd:sequence>
          </xsd:complexType>
      </xsd:element>
      </xsd:schema>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-12
        • 1970-01-01
        • 2019-12-29
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多