开发者

Restrict Element Values Based on Attribute

I want to restrict the type of an Element based on the valu开发者_Python百科e of an Attribute, like this:

<Data type="decimal"> 44.00 </Data>

<Data type="date"> 2008-02-01 </Data>

Can a Schema be defined that does this?


It's a well known fact that, XML-Schema can't do this,

validation of an element based on the other element is treated as ambiguous, so not possible. But if you want, you can validate both type of data-types ignoring the conditional validation, something like this:

Define a new data type with custom name, and copy paste the pattern written below.

  <xs:simpleType name="new_type">
    <xs:restriction base="xs:string">
      <xs:pattern value="(([0-9]+)[.]([0-9]+))|(([0][1-9]|[1][0-2])/([0][1-9]|[1-2][0-9]|[3][0-1])/[1-2][0-9][0-9][0-9])"/>
    </xs:restriction>
  </xs:simpleType>

This will accept the Data of type decimal or date, but you cannot validate it with the attribute values,

The alternative solution is to use Schematron, but you won't get so handy and user-friendly tool for that as you get in-case of XSD and XSLT.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜