XML Schema - restrictions and attributes in one component

371 views Asked by At

I'm learning how to create xml/xsd documents and I'm stuck now. I don't know how to connect this restrictions in one component:

<xsd:simpleType name="unitType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="g"/>
        <xsd:enumeration value="Gigabyte"/>
        <xsd:enumeration value="mAh"/>
        <xsd:enumeration value="Year"/>
    </xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="ProducedContent">
    <xsd:restriction base="xsd:integer">
        <xsd:minInclusive value="1970"/>
        <xsd:maxInclusive value="2015"/>
    </xsd:restriction>
</xsd:simpleType>


<xsd:complexType name="ProducedContent">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
            <xsd:attribute name="unit" type="unitType" use="required"/>
        </xsd:extension>
    </xsd:simpleContent>
</xsd:complexType>

I know that it will not work, but I must set above restrictions to this element:

<Produced unit="Year">2014</Produced>
1

There are 1 answers

0
kjhughes On BEST ANSWER

The restrictions you request on this XML instance:

<Produced unit="Year">2014</Produced>

Will be enforced by this XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            version="1.0">

  <xsd:element name="Produced">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="ProducedContent">  
          <xsd:attribute name="unit" type="unitType"/>  
        </xsd:extension>  
      </xsd:simpleContent>  
    </xsd:complexType>  
  </xsd:element>

  <xsd:simpleType name="unitType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="g"/>
      <xsd:enumeration value="Gigabyte"/>
      <xsd:enumeration value="mAh"/>
      <xsd:enumeration value="Year"/>
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="ProducedContent">
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="1970"/>
      <xsd:maxInclusive value="2015"/>
    </xsd:restriction>
  </xsd:simpleType>

</xsd:schema>