David542
David542

Reputation: 110267

XML/XSD for type with extension and attribute with restriction

I have the following XML possibilities:

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

What would be the correct XSD element for this? So far I have:

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

How would I further add the restriction of the platform value to be either "iTunes" or "Netflix". That is, where would I add:

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

Upvotes: 1

Views: 72

Answers (1)

kjhughes
kjhughes

Reputation: 111630

Here is your XSD modified to accept your XML as valid:

<?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>

Note that your declaration of @IsSubscriptionPlatform allows it to be empty. If you don't want that, remove the ?, or use enumerations. Or, if you're free to change the XML design, go with true and false instead and simplify your declaration to this:

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

Upvotes: 1

Related Questions