简体   繁体   English

XSD-根据父元素中的属性值验证属性值

[英]XSD - Validate attribute value based on attribute value in parent element

Is it possible to define an XSD structure such that and attribute on an element can have a certain value only if an attribute on a parent (direct/indirect) element has a certain value? 是否可以定义一个XSD结构,以便仅当父(直接/间接)元素上的属性具有特定值时,元素上的和属性才能具有特定值?

Example: 例:

<root>
    <child1 myAttr="true">
        <child2>
            <child3 otherAttr="false" /> <!-- 'otherAttr' can only be 'false' if 'myAttr' is 'true' -->
        </child2>
    </child1>
</root>

Pseudo Solution: 伪解决方案:

To add something like <rule condition="@otherAttr == true && //child1/@myAttr != false" /> to the definition of the 'child3' complex type... 要在'child3'复杂类型的定义中添加<rule condition="@otherAttr == true && //child1/@myAttr != false" />之类的内容...

Example: 例:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       xmlns="http://My.Schema.Namespace" 
       targetNamespace="http://My.Schema.Namespace">

<xs:element name="root">
  <xs:sequence>
      <xs:element name="child1" type="child1Type" />
  </xs:sequence>
</xs:element>

<xs:complexType name="child1Type">
  <xs:sequence>
    <xs:element name="child2" type="child2Type" />
  </xs:sequence>
  <xs:attribute name="myAttr" type="xs:boolean" use="optional" default="false" />
</xs:complexType>

<xs:complexType name="child2Type">
  <xs:sequence>
    <xs:element name="child3" type="child3Type" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="child3Type">
  <xs:attribute name="otherAttr" type="xs:boolean" use="optional" default="true" /> 
  <rule condition="@otherAttr == true && //child1/@myAttr != false" />
</xs:complexType>

To define any kind of cross-validation you need XSD 1.1, which allows arbitrary assertions. 要定义任何类型的交叉验证,您需要XSD 1.1,它允许任意断言。 An assertion has access to the subtree of the element where the assertion is placed, and not to its ancestors, so the assertion in your case needs to go on the child1 element. 断言可以访问放置断言的元素的子树,而不能访问其祖先,因此断言必须在child1元素上进行。 You haven't explained very clearly what the condition actually is, but it would be something like 您还没有很清楚地说明实际情况是什么,但这可能是

<xs:assert test="if (@myAttr = 'true') then child2/child3/@otherAttr = 'false' else true()"/>

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

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