简体   繁体   English

根据属性限制元素值

[英]Restrict Element Values Based on Attribute

I want to restrict the type of an Element based on the value of an Attribute, like this: 我想基于Attribute的值来限制Element的类型,如下所示:

<Data type="decimal"> 44.00 </Data> <Da​​ta type =“ decimal”> 44.00 </ Data>

<Data type="date"> 2008-02-01 </Data> <Da​​ta 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, 众所周知,XML-Schema无法做到这一点,

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. 另一种解决方案是使用Schematron,但是如果您使用的是XSD和XSLT,您将不会得到那么方便和用户友好的工具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM