2016-11-20 92 views
1

属性我有以下XML可能性:XML/XSD与扩展类型和限制

  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> 

回答

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"/>