Question

How to verify XML element with mixed content? Element can contain string "Undefined" or float values. I think it can be done thru string Restriction with pattern, but maybe a better way exists?

Was it helpful?

Solution

Resolved by Union element using.

Consider below example: A sample XML defines calender dates.

<Calender>
  <Date>
    <Date>01</Date>
    <Month>Jan</Month>
    <Year>2013</Year>
  </Date>
  <Date>
    <Date>31</Date>
    <Month>01</Month>
    <Year>2013</Year>
  </Date>
</Calender>

Since Month element has both types Int and String .. It can be resolved like the way below XSD is written:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Calender" type="Calender"/>
  <xs:complexType name="Calender">
    <xs:sequence>
      <xs:element maxOccurs="unbounded" name="Date" type="Date"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="Date">
    <xs:sequence>
      <xs:element name="Date" type="xs:unsignedByte" />
      <xs:element name="Month" type="Month" />
      <xs:element name="Year" type="xs:unsignedShort" />
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="Month">
    <xs:union memberTypes="MonthNum MonthVal" />
  </xs:simpleType>
  <xs:simpleType name="MonthNum">
    <xs:restriction base="xs:int">
      <xs:minInclusive value="01"/>
      <xs:maxInclusive value="31"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="MonthVal">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Jan"/>
      <xs:enumeration value="Feb"/>
      <xs:enumeration value="Mar"/>
      <xs:enumeration value="Dec"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top