<选择>的使用方法来自 XSD 中具有相同元素的 2 个值的复杂类型? [英] How to use &lt;choice&gt; from complex types in XSD with 2 values of a same element?

查看:22
本文介绍了<选择>的使用方法来自 XSD 中具有相同元素的 2 个值的复杂类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证 2 种 XML过滤器"块,它们的形状"包含两个值之一:空"或圆":

I'd like to validate XML "filter" blocks of 2 kinds, whose "shape" contains either of 2 values : "empty" or "circle" :

  • 如果为空",则块应仅包含形状".
  • 如果需要圆"、中心"和直径".

XML 示例:

<filter>
  <shape>empty</shape>
</filter>

<filter>            
  <shape>circle</shape>
  <center>10.3</center>
  <diameter>5.1</diameter>
<filter>

我已经尝试过这个 XSD 架构:

I have tried this XSD schema :

<xs:element name="filter">
  <xs:complexType>
    <xs:choice>
      <xs:all>
        <xs:element name="shape" type="xs:string" fixed="circle"/>
        <xs:element name="center" type="xs:decimal"/>
        <xs:element name="diameter" type="xs:decimal"/>
      </xs:all>
      <xs:element name="shape" type="xs:string" fixed="empty"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

不成功... xmllint 抱怨:

Unsuccessfully... xmllint complains :

mytest.xsd:160: element all: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}choice':内容无效.预期是 (annotation?, (element | group | choice | sequence | any)*).

mytest.xsd:160: element all: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}choice': The content is not valid. Expected is (annotation?, (element | group | choice | sequence | any)*).

WXS 架构 mytest.xsd 编译失败

WXS schema mytest.xsd failed to compile

如果我用 xs:sequence 替换 xs:all,它会说:

If I replace xs:all with xs:sequence, it says :

mytest:158: element complexType: Schemas parser error : local complex type: The content model is not determinist.

mytest:158: element complexType: Schemas parser error : local complex type: The content model is not determinist.

WXS 架构 mytest.xsd 编译失败

WXS schema mytest.xsd failed to compile

如果可能的话,如何编写这段 XSD?

How to write this piece of XSD - if possible ?

我知道如果我的 XML 使用shape0"而不是shape"来表示空":

I know that if my XML was using a "shape0" instead of "shape" for "empty" :

<filter>
  <shape0>empty</shape0>
</filter>

<filter>            
  <shape>circle</shape>
  <center>10.3</center>
  <diameter>5.1</diameter>
<filter>

它会通过以下方式进行验证:

it would validate fine with :

<xs:element name="filter">
  <xs:complexType>
    <xs:choice>
      <xs:sequence>
        <xs:element name="shape" type="xs:string" fixed="circle"/>
        <xs:element name="center" type="xs:decimal"/>
        <xs:element name="diameter" type="xs:decimal"/>
      </xs:sequence>
      <xs:element name="shape0" type="xs:string" fixed="empty"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

但是,不幸的是,我的 XML 使用了相同的关键字...

But, unfortunately, my XML uses the same keyword...

推荐答案

在 XSD 1.0 中,无法为依赖于内容的结构定义规则.

In XSD 1.0, it's not possible to define rules for the structure that depend on the content.

在 XSD 1.1 中,您可以使用断言来做到这一点,例如<xs:assert test="not(shape='empty' and (center or diameter))"/>

In XSD 1.1 you can do this with assertions, e.g. <xs:assert test="not(shape='empty' and (centre or diameter))"/>

这篇关于<选择>的使用方法来自 XSD 中具有相同元素的 2 个值的复杂类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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