XML Schema:为什么<xs:all>不能有<choice>子元素?怎么能绕过这个?

spl*_*tor 17 xml xsd

我试图创建一个架构<property>元素必须有一个<key>子元素,和一个<val>,<shell>或者<perl>和一个可选的<os><condition>,和子元素的顺序并不重要.

以下是有效<property>元素的示例:

<property>
  <key>A</key>
  <val>b</val>
</property>

<property>
  <key>A</key>
  <val>b</val>
  <os>Windows</os>
</property>

<property>
  <condition>a == 1</condition>
  <key>A</key>
  <perl>1+1</perl>
  <os>unix</os>
</property>
Run Code Online (Sandbox Code Playgroud)

理想情况下,我想<xs:all>为此使用:

<xs:element name="property">
  <xs:complexType>
    <xs:all>
      <xs:element name="key" type="xs:string" />
      <xs:choice>
        <xs:element name="val" type="xs:string" />
        <xs:element name="perl" type="xs:string" />
        <xs:element name="shell" type="xs:string" />
      </xs:choice>
      <xs:element name="os" type="xs:string" minOccurs="0" />
      <xs:element name="condition" type="xs:string" minOccurs="0" />
    </xs:all>
  </xs:complexType>
</xs:element>
Run Code Online (Sandbox Code Playgroud)

但我发现<xs:all>只能包含<xs:element>而不能包含<xs:choice>.有人可以解释为什么会这样吗?

更重要的是,有人可以提供验证这种<property>元素的方法吗?

我可以把三个要素- <val>,<perl><shell>-作为可选元素<xs:all>,但我想要的架构验证有且只有三个中的一个元素存在.可以这样做吗?

Ada*_*kes 22

我认为这有点好,因为"选择"现在是它自己的元素(typeFacet),但不能直接使用,因为它是抽象的.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="property">
    <xs:complexType>
      <xs:all>
        <xs:element name="key" type="xs:string" />
        <xs:element ref="typeFacet" />
        <xs:element name="os" type="xs:string" minOccurs="0" />
        <xs:element name="condition" type="xs:string" minOccurs="0" />
      </xs:all>
    </xs:complexType>
  </xs:element>

  <xs:element name="typeFacet" abstract="true" />
  <xs:element name="val" type="xs:string"   substitutionGroup="typeFacet" />
  <xs:element name="perl" type="xs:string" substitutionGroup="typeFacet" />
  <xs:element name="shell" type="xs:string" substitutionGroup="typeFacet" />
</xs:schema>
Run Code Online (Sandbox Code Playgroud)


13r*_*ren 7

基于newt关于使用替换组进行选择的注释(使用xmllint测试):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="property">
    <xs:complexType>
      <xs:all>
        <xs:element name="key" type="xs:string" />
        <xs:element ref="val"/>
        <xs:element name="os" type="xs:string" minOccurs="0" />
        <xs:element name="condition" type="xs:string" minOccurs="0" />
      </xs:all>
    </xs:complexType>
  </xs:element>

  <xs:element name="val" type="xs:string"/>
  <xs:element name="perl" type="xs:string" substitutionGroup="val" />
  <xs:element name="shell" type="xs:string"  substitutionGroup="val" />
</xs:schema>
Run Code Online (Sandbox Code Playgroud)