xml - 需要任意元素序列中的特定元素

标签 xml xsd xsd-validation

我想创建一个像这样的 XSD:

<xs:schema>
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
        <xs:element name="b" minOccurs="1" maxOccurs="unbounded"/>
        <xs:any minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

说明:

元素 a 必需,位于元素内部:

  • 任意元素 0 到无穷次。
  • 元素 b 1 到无穷次。
  • 任意元素 0 到无穷次。

我尝试使用choice , all等等,但我没有找到任何解决方案。

来自 XmlSpy 的错误:

<any...> makes the content model non-deterministic against <xs:element name="b" ...>. Possible causes: name equality, overlapping occurrence or substitution groups.

最佳答案

您的 XSD 实际上试图表明,只要至少有一个 b 元素,a 就可以具有任意子元素序列。

XSD 1.0 无法强制执行此类约束,因为任何这样做的尝试(包括您提供的尝试)都会违反唯一粒子原则。

XSD 1.1 可以通过一个简单的断言强制执行这样的约束,声明 b 存在于 axsd:any 子级中。

XSD 1.1

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning"
  vc:minVersion="1.1">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:any processContents="skip" maxOccurs="unbounded"/>
      </xs:sequence>
      <xs:assert test="b"/>
    </xs:complexType>
   </xs:element>
</xs:schema>

关于xml - 需要任意元素序列中的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32090194/

相关文章:

java - JAX-WS (JAXB) : How to marshall java. util.Date 到 xs :anyType as xs:date instead of xs:dateTime?

sql - 使用命名空间修改 xml

java - JAXB 是否支持 xsd :restriction?

c# - xs :key, 为什么当键值不是键引用的成员时验证通过?

c# - 将 XDocument 转换为流

xml - 如何在 XSD 中使用 targetNamespace 来验证 XML

xml - JAXB |带有图像 :image-Tags using "http://www.google.com/schemas/sitemap-image/1.1" 的 Google 站点地图

c# - 从 XML、XSD 或 C# 类生成 LINQ DataContext

xml - 允许随机排序的 XSD 架构

xml - Xquery 处理错误 (saxon) XPST0003 : Left operand of '>' needs parentheses