簡體   English   中英

XSD-根據父元素中的屬性值驗證屬性值

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

是否可以定義一個XSD結構,以便僅當父(直接/間接)元素上的屬性具有特定值時,元素上的和屬性才能具有特定值?

例:

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

偽解決方案:

要在'child3'復雜類型的定義中添加<rule condition="@otherAttr == true && //child1/@myAttr != false" />之類的內容...

例:

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

要定義任何類型的交叉驗證,您需要XSD 1.1,它允許任意斷言。 斷言可以訪問放置斷言的元素的子樹,而不能訪問其祖先,因此斷言必須在child1元素上進行。 您還沒有很清楚地說明實際情況是什么,但這可能是

<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