简体   繁体   English

构建需要元素或属性(但不是全部)的XML模式

[英]Build XML schema that requires element or attribute, but not both

I would like to create an XML schema with the following constraints: 我想创建一个具有以下约束的XML模式:

  • The root element shall be list . 根元素应为list
  • list contains a set of node elements. list包含一组node元素。
  • a node element can have a value attribute. node元素可以具有value属性。
  • a node element can contain a value element. node元素可以包含value元素。
  • a node element can have only (and exactly ) one value attribute or value element. 一个node元素只能(并且恰好 )具有一个value属性或value元素。

Here is an example of a valid XML that verify above constraints: 这是验证上述约束的有效XML的示例:

<?xml version="1.0" ?>
<list>
    <node id="1" value="A" />
    <node id="2">
        <value>B</value>
    </node>
</list>

I tried the following XSD Schema: 我尝试了以下XSD架构:

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="list">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="node" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType mixed="true">
            <xs:sequence>
              <xs:element type="xs:string" name="value" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute type="xs:byte" name="id" use="optional"/>
            <xs:attribute type="xs:string" name="value" use="optional"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This XSD schema verify my first and the second constraint, but not the third one. 这个XSD模式验证了我的第一个和第二个约束,但没有验证第三个约束。

The following XML example is not valid according to my constraints, however, it is valid against my previous XSD schema. 根据我的约束,以下XML示例无效,但是,对于我以前的XSD模式而言,它是有效的。

<?xml version="1.0" ?>
<list>
    <node id="2" value="A" /> <!-- valid -->
    <node id="4">
        <value>D</value>
    </node><!-- valid -->
    <node id="1" /><!-- not valid, missing value -->
    <node id="3" value="B">
        <value>C</value>
    </node><!-- not valid, both attribute and element are declared -->
</list>

How can I change my schema to verify all my constraints? 如何更改架构以验证所有约束?

Thanks in advance. 提前致谢。

This is not possible using XSD 1.0. 使用XSD 1.0不可能做到这一点。

It should be possible with XSD 1.1 using assertions: 使用断言的XSD 1.1应该应该是可能的:

<xs:assert test="not(@value and value)"/>

that fails if @value and value are both present. 如果@valuevalue都存在,则失败。 To check that there is exactly one attribute or sub-element use: 要检查是否只有一个属性或子元素,请使用:

<xs:assert test="count((value, @value))=1"/>

as suggested by Michael Kay in the comments. 正如迈克尔·凯(Michael Kay)在评论中建议的那样。

It might be possible using some other XML validation technology like Schematron. 使用其他一些XML验证技术(例如Schematron)可能是可能的。

You can create first type with required attribute and second type with required element. 您可以使用必需的属性创建第一种类型,并使用必需的元素创建第二种类型。 Then use choice compositor containing both types. 然后使用包含两种类型的选择合成器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM