XML 模式:为什么不能 &lt;xs:all&gt;有<选择>孩子们?如何绕过? [英] XML Schema: Why can&#39;t &lt;xs:all&gt; have &lt;choice&gt; children? and how can this be bypassed?

查看:31
本文介绍了XML 模式:为什么不能 &lt;xs:all&gt;有<选择>孩子们?如何绕过?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 元素创建架构,该元素必须具有 子元素和 之一<val> 以及可选的 <condition>,并且子元素的顺序无关紧要.

I'm trying to create a schema for a <property> element which must have a <key> sub-element, and one of <val>, <shell> or <perl> and an optional <os> or <condition>, and the order of the sub-elements doesn't matter.

以下是有效 元素的一些示例:

Here are some sample for valid <property> elements:

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

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

Ideally, I thought of using <xs:all> for this:

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

但我发现 只能包含 而不是 代码>.有人能解释一下这是为什么吗?

But I found out that <xs:all> can contain only <xs:element> and not <xs:choice>. Can someone explain why is it?

更重要的是,有人能提供一种方法来验证这样的 元素吗?

More importantly, can someone provide a way to validate such a <property> element?

我可以将三个元素 - - 作为可选元素放入<xs:all>,但我希望架构验证元素中存在三个中的一个并且仅存在一个.可以这样做吗?

I can put the three elements - <val>, <perl> and <shell> - as optional elements in the <xs:all>, but I want the schema to validate that one and only one of the three exists in the element. Can this be done?

推荐答案

我觉得这样更好一些,因为选择"现在是它自己的元素(typeFacet),但不能直接使用,因为它是抽象的.

I think this is a bit better, as the "choice" is now it's own element (typeFacet), but cannot be used directly as it is abstract.

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

这篇关于XML 模式:为什么不能 &lt;xs:all&gt;有<选择>孩子们?如何绕过?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆