开发者

Mixed types in XSD [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopene开发者_高级运维d, visit the help center. Closed 10 years ago.

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?


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜