XML 模式属性依赖项 [英] XML schema attribute dependancies

查看:45
本文介绍了XML 模式属性依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否可以在 XML xsd 模式中声明依赖属性...

I'd like to ask if it's possible in XML xsd schema declare dependant attributes...

示例:

<xsd:simpleType name="packCategories">
 <xsd:restriction base="xsd:byte">
  <xsd:minInclusive value="0"/>
  <xsd:maxInclusive value="4"/>
 </xsd:restriction>
</xsd:simpleType>

<xs:element name="pack">
 <xs:complexType>
   <!-- elements go here -->
   <xs:attribute type="packCategories" name="category" use="required"/>
   <xs:attribute type="xs:string" name="explanation" use="optional"/>
 </xs:complexType>
</xs:element>

这里一切似乎都很好,但是,如果类别属性等于 4,我希望说明属性为 MANDATORY.这可能吗?或许还有元素?

Everything seems fine here, HOWEVER, i want the explanation attribute to be MANDATORY if category attribute is equal to 4. Is that possible? Maybe with elements then?

推荐答案

如果你使用的是 XML Schema 1.0,你不能在模式中表达这样的约束,但你可以使用 Schematron 或在应用程序级别检查.

If you're using XML Schema 1.0, you cannot express such a constraint in the schema, but you could use Schematron or check it at the application level.

如果您使用的是 XML Schema 1.1,您可以通过 XPath 2.0 使用 xs:assert > 指定共现约束,如下所示:

If you're using XML Schema 1.1, you can specify co-occurrence constraints via XPath 2.0 using xs:assert like this:

  <xs:element name="pack">
    <xs:complexType>
      <!-- elements go here -->
      <xs:attribute type="packCategories" name="category" use="required"/>
      <xs:attribute type="xs:string" name="explanation" use="optional"/>
      <xs:assert test="@explanation or @packCategories != 4"/>
    </xs:complexType>
  </xs:element>

这篇关于XML 模式属性依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆