如何在 xsd:choice 元素中定义多个名称相同但类型不同的元素? [英] How define several elements with same name, but different type in xsd:choice element?

查看:56
本文介绍了如何在 xsd:choice 元素中定义多个名称相同但类型不同的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能以某种方式定义一个可以验证此类 xml 的 xsd 方案:

<item ItemType="SimpleMessage" Caption="最简单的消息"/><item ItemType="ComplexMessage" SomeAttr="value"><item_data>一些文本</item_data></项目></item_list>

问题是我没有找到任何可能性来定义 smth 像:

 <xsd:complexType><xsd:选择><xsd:element name="item" type="SimpleMessType"/><xsd:element name="item" type="ComplexMessType"/></xsd:choice></xsd:complexType></xsd:element>

但我需要检查一下,SimpleMessage 没有子元素或附加属性 :(

解决方案

正如前面的答案已经提到的,您可以在 XSD 1.0 中通过使用 xsi:type 属性而不是定义来轻松地做到这一点具有相同功能的新 ItemType 属性.

XSD 1.1 包含一个结构,旨在使支持此类情况更容易,对于出于某种原因不想以这种方式使用 xsi:type 的人:条件类型分配.本质上,它允许元素声明具有简单的 XPath/typename 对序列;XPath 表达式按顺序计算,当计算结果为 true 时,元素与相应的类型相关联.XPaths 有一些限制,以禁止向前查看元素的后代或查看或查看 XML 文档的其他部分(第一个帮助保持可能,一旦扫描遇到开始标记,就可以知道哪个类型要用于验证元素;第二个有助于保持验证上下文无关),因此本质上测试只能是对属性值的测试.你的例子可以这样写:

<xs:alternative test="@ItemType='SimpleMessage'" type="SimpleMessType"/><xs:alternative test="@ItemType='SimpleMessage'" type="ComplexMessType"/><xs:alternative type="xs:error"/></xs:element>

第三种选择确保必须遇到您预期的情况之一,元素才有效.如果此处省略,则如果两个测试表达式都不为真,则元素将被指定为 item 的声明类型,在本例中为 xs:anyType.

Is it possible in some way, to define an xsd scheme which could validate such xml:

<item_list>
  <item ItemType="SimpleMessage" Caption="Simplest message"/>
  <item ItemType="ComplexMessage" SomeAttr="value">
    <item_data>some text</item_data>
  </item>
</item_list>

Problem is that i havn't find any possibility to define smth like:

  <xsd:element name="Items">
      <xsd:complexType>
        <xsd:choice>
          <xsd:element name="item" type="SimpleMessType"/>
          <xsd:element name="item" type="ComplexMessType"/>
        </xsd:choice>
      </xsd:complexType>
  </xsd:element>

But i need to check, that SimpleMessage has no child elements or additional attrs :(

解决方案

As earlier answers have already mentioned, you can do this easily enough in XSD 1.0 by using the xsi:type attribute instead of defining a new ItemType attribute with the same functionality.

XSD 1.1 includes a construct designed to make it easier to support cases like this one, for people who for whatever reason don't want to use xsi:type this way: conditional type assignment. Essentially it allows an element declaration to have simple sequence of XPath / typename pairs; the XPath expressions are evaluated in sequence and when one evaluates to true, the element is associated with the corresponding type. There are restrictions on the XPaths to prohibit looking ahead into the element's descendants or looking up or out into other parts of the XML document (the first helps keep it possible to know, as soon as a scan encounters a start-tag, which type to use for validating an element; the second helps keep validation context-free), so essentially the tests can only be tests on attribute values. Your example can be written thus:

<xs:element name="item">
  <xs:alternative test="@ItemType='SimpleMessage'" type="SimpleMessType"/>
  <xs:alternative test="@ItemType='SimpleMessage'" type="ComplexMessType"/>
  <xs:alternative type="xs:error"/>
</xs:element>

The third alternative ensures that one of your expected cases must be encountered, for the element to be valid. If it were omitted here, then if neither of the test expressions were true, the element would be assigned the declared type of item, in this case xs:anyType.

这篇关于如何在 xsd:choice 元素中定义多个名称相同但类型不同的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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