How to represent an element with either decimal or empty contents in schema? Can enumeration be empty too?

Roseanne Zhang

Q. How to represent an element with either decimal or empty contents in schema? Can enumeration be empty too? A: Yes, we can! It is not easy, but I finally found the solution.

Enumeration is easy. Just add an empty item. However, decimal is hard. I have to use union.

Attention, union does not take complexType. Here is the xml source and xschema for it. It is validated by SAX. XML Source

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="op1.xsd">
  <StructureLength>
    <Value Comment="Structure value comment">1.1</Value>
    <Unit Comment="Structure unit comment">m</Unit>
  </StructureLength>
  <StructureLength>
    <Value Comment="Structure value comment" />
    <Unit Comment="Structure unit comment" />
  </StructureLength>
</root>
XML Schema Source
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema attributeFormDefault="unqualified" 
    elementFormDefault="qualified" 
    version="1.0" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="root">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="StructureLength" type="DistanceStructure" maxOccurs="unbounded" />
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>

  <xsd:complexType name="DistanceStructure">
    <xsd:sequence>
      <xsd:element name="Value">
        <xsd:complexType>
          <xsd:simpleContent>
            <xsd:extension base="unionType">
              <xsd:attribute name="Comment" type="xsd:string"/>
            </xsd:extension>
          </xsd:simpleContent>
        </xsd:complexType>
      </xsd:element>
      
      <xsd:element name="Unit">
        <xsd:complexType>
          <xsd:simpleContent>
            <xsd:extension base="DistanceUnitType">
              <xsd:attribute name="Comment" type="xsd:string"/>
            </xsd:extension>
          </xsd:simpleContent>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
  </xsd:complexType>

  <xsd:simpleType name="DistanceUnitType">
    <xsd:restriction base="xsd:string">
      <xsd:enumeration value="mm" />
      <xsd:enumeration value="m" />
      <xsd:enumeration value="km" />
      <xsd:enumeration value="AU" />
      <xsd:enumeration value="ly" />
      <xsd:enumeration value="pc" />
      <xsd:enumeration value="" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="emptyType" >
    <xsd:restriction base="xsd:string">
      <xsd:length value="0" />
    </xsd:restriction>
  </xsd:simpleType>

  <xsd:simpleType name="unionType">
    <xsd:union memberTypes="xsd:decimal emptyType" />
  </xsd:simpleType>

</xsd:schema>
I defined my own DistanceUnitType¡£ Try them out, it is validated. thanks!
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact