【问题标题】:XML/XSD for type with extension and attribute with restrictionXML/XSD 用于带扩展名的类型和带限制的属性
【发布时间】:2017-04-03 18:51:45
【问题描述】:

我有以下 XML 可能性:

  1. <Platform>iTunes</Platform>
  2. <Platform IsSubscriptionPlatform="True">Netflix</Platform>

什么是正确的 XSD 元素?到目前为止,我有:

<xs:element name="Platform">
  <xs:complexType>
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute name="IsSubscriptionPlatform" use="optional">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:pattern value="(True|False)?" />
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
</xs:element>

我将如何进一步将平台值的限制添加为“iTunes”或“Netflix”。也就是说,我会在哪里添加:

<xs:restriction base="xs:string">
  <xs:enumeration value="iTunes" />
  <xs:enumeration value="Netflix" />
</xs:restriction>

【问题讨论】:

    标签: xml xsd lxml


    【解决方案1】:

    您的 XSD 已修改为接受您的 XML 为有效:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
               elementFormDefault="qualified">
    
      <xs:simpleType name="PlatformCompany">
        <xs:restriction base="xs:string">
          <xs:enumeration value="iTunes" />
          <xs:enumeration value="Netflix" />
        </xs:restriction>    
      </xs:simpleType>
    
      <xs:element name="Platform">
        <xs:complexType>
          <xs:simpleContent>
            <xs:extension base="PlatformCompany">
              <xs:attribute name="IsSubscriptionPlatform" use="optional">
                <xs:simpleType>
                  <xs:restriction base="xs:string">
                    <xs:pattern value="(True|False)?" />
                  </xs:restriction>
                </xs:simpleType>
              </xs:attribute>
            </xs:extension>
          </xs:simpleContent>
        </xs:complexType>
      </xs:element>
    
    </xs:schema>
    

    请注意,@IsSubscriptionPlatform 的声明允许它为空。如果您不想这样做,请删除?,或使用枚举。或者,如果您可以随意更改 XML 设计,请改用 truefalse 并将您的声明简化为:

    <xs:attribute name="IsSubscriptionPlatform" use="optional" type="xs:boolean"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-20
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多