2012-12-11 165 views
1

在下面的XSD示例中,如果“Faron”元素具有“TestMethod”属性= 1,那么子元素 faronsettings必须具有属性“test1 “并有价值。如何根据父元素属性在xsd中设置子元素属性

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

可能重复[XSD:如何根据某个标记的值验证XML文件?](http://stackoverflow.com/questions/1879652/xsd-how-to-validate-the-xml-file-根据价值的某些标记) –

+0

可能的重复http://stackoverflow.com/questions/4126988/restricting-xml-elements-based-on-another-element-via-xsd/4141569#4141569 –

+0

好吧,所以不可能与XSD ..可能是“Schematron” – Rusty

回答

0

使用带有条件类型分配和断言的XSD 1.1。

或者使用Schematron。

或设计您的词汇表,以便可以用元素类型表示有效性约束,而不需要对属性值进行上诉。

你付出你的钱,你选择。

+0

如何/在哪里使用XSD 1.1?据我所知,它还没有用于开发者!如果您知道其在任何SDK(Eclips,MSvisual studio等)中的使用情况,请提供详细信息 –

+0

Xerces J和Saxon-EE支持XSD 1.1;许多人发现使用Oxygen(它们捆绑这两个)来管理XSD 1.1验证很方便。如果你使用的SDK没有,你可能会考虑游说维护者前进。 –

+0

啊我用.net!所以现在我要接触那颗明星!我也无法升级我的系统!无论如何感谢信息! –