基于属性值的条件(XML Schema)

时间:2012-01-10 09:49:48

标签: xml xsd

是否可以在XML Schema中定义基于属性值的条件?例如,在test@attrib="one"时,我希望one-element被允许并且是强制性的,或者在test@attrib="two"时,我希望two-element被允许并且是强制性的。

例如,有效文件是:

<root>
    <test attrib="one"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <one-element>
    </one-element>
</root>

<root>
    <test attrib="two"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <two-element>
    </two-element>
</root>

错误的文件:

<root>
    <test attrib="one"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
</root>

<root>
    <test attrib="two"/>
    <some-element-1/>
    <some-element-2/>
    ...
    <some-element-n/>
    <one-element>
    </one-element>
</root>

XSD可以吗?

1 个答案:

答案 0 :(得分:10)

不属于同一类型。您需要为每个不同的选项定义不同的类型。

<强>更新

要在架构中重复使用类型定义:

<?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:complexType>
      <xs:choice>
        <xs:element name="test1" type="test1Type" />
        <xs:element name="test2" type="test2Type" />
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <!-- define the two root types -->
  <xs:complexType name="test1Type">
    <xs:all>
      <xs:element name="some-element-1" type="some-element-1Type" />
      <xs:element name="some-element-2" type="some-element-2Type" />
      <xs:element name="some-element-3" type="some-element-3Type" />
      <xs:element name="one-element" type="one-elementType" />
    </xs:all>
    <xs:attribute name="attrib" type="xs:string" fixed="one" />
  </xs:complexType>

  <xs:complexType name="test2Type">
    <xs:all>
      <xs:element name="some-element-1" type="some-element-1Type" />
      <xs:element name="some-element-2" type="some-element-2Type" />
      <xs:element name="some-element-3" type="some-element-3Type" />
      <xs:element name="two-element" type="two-elementType" />
    </xs:all>
    <xs:attribute name="attrib" type="xs:string" fixed="two" />
  </xs:complexType>

  <!-- Define re-usable types-->
  <xs:complexType mixed="true" name="some-element-1Type"/>
  <xs:complexType mixed="true" name="some-element-2Type"/>
  <xs:complexType mixed="true" name="some-element-3Type"/>
  <xs:complexType mixed="true" name="one-elementType"/>
  <xs:complexType mixed="true" name="two-elementType"/>

</xs:schema>

这将验证:

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
  <test1 attrib="one">
    <some-element-1>sadas</some-element-1>
    <some-element-2>sadas</some-element-2>
    <some-element-3>sadas</some-element-3>
    <one-element>sadas</one-element>
  </test1>
</root>

<?xml version="1.0" encoding="utf-8" ?>
<root xmlns="http://My.Schema.Namespace">
  <test2 attrib="two">
    <some-element-1>sadas</some-element-1>
    <some-element-2>sadas</some-element-2>
    <some-element-3>sadas</some-element-3>
    <two-element>sadas</two-element>
  </test2>
</root>