簡體   English   中英

如何基於XSD中的父元素屬性制作所需的子元素屬性

[英]How to make child element attributes required based on parent element attribute in xsd

在XSD的以下示例中,我想要的是,如果“ Faron”元素的“ TestMethod”屬性= 1,則子元素faronsettings必須具有屬性“ test1”並具有值。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Faron">
<xs:complexType>
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="Application">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="FaronSettings">
            <xs:complexType>
              <xs:attribute name="test1" type="xs:string" use="optional" />
              <xs:attribute name="test2" type="xs:string" use="optional" />
              <xs:attribute name="test3" type="xs:string" use="optional" />
              <xs:attribute name="test4" type="xs:string" use="optional" />
            </xs:complexType>
          </xs:element>
        </xs:sequence>
        <xs:attribute name="TestMethod" type="TestType" use="required" />
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="TestType">
<xs:restriction base="xs:string">
  <xs:minLength value="1" />
  <xs:enumeration value="1"/>
  <xs:enumeration value="2"/>
  <xs:enumeration value="3"/>
  <xs:enumeration value="4"/>
</xs:restriction>
 </xs:simpleType>
</xs:schema>

將XSD 1.1與條件類型分配和斷言一起使用。

或使用Schematron。

或者設計您的詞匯表,以便可以在不影響屬性值的情況下根據元素類型來表示有效性約束。

您付錢,選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM