xml - 如何声明一个 complexType 只有一个子元素?

标签 xml xsd xsd-validation xml-validation

当使用XML Schema声明一个complexType只有一个子元素时,以下三种方式都可以达到目的:

<xs:complexType> <xs:sequence> <xs:element ref="somevalue"/> </xs:sequence> </xs:comlexType>
<xs:complexType> <xs:choice>   <xs:element ref="somevalue"/> </xs:choice>   </xs:comlexType>
<xs:complexType> <xs:all>      <xs:element ref="somevalue"/> </xs:all>      </xs:comlexType>

显然,sequencechoiceall 不是单个元素所必需的,因为它们应该用于指示顺序的多个元素。 是否有更简洁的方法来声明只有一个子元素的 complexType(即消除使用 sequence 的方法,allchoice,不知何故。)

最佳答案

只是消除 xs:sequencexs:choicexs:all 不是一种选择:

  <xs:complexType name="cType">
    <xs:element name="e"/> 
  </xs:complexType>

无效。

参见 XML Representation of Complex Type Definitions其中complexType的内容模型定义如下:

(annotation?, (simpleContent | complexContent | ((group | all | choice | sequence)?, ((attribute | attributeGroup)*, anyAttribute?))))

没有规定 elementcomplexType 的直接子级。

关于xml - 如何声明一个 complexType 只有一个子元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19223099/

相关文章:

java - 以编程方式将 use ="required"字段添加到 XML 架构属性

xml - 允许/限制基于其他属性的 XML 属性

sql - 在 SQL 中读取 XML 属性时返回 '0' INT

xsd - 将any 添加到complexType

xml - JAXB 2 注释和 XSD

xml - 在 XSD 中定义逗号分隔值

包含非空字符串和元素的混合内容复杂类型的 XML 架构

java - 使用 XML 注释

python - 使用 python mindom 读取具有不同编码的 XML

java - xsd 未在某些系统上验证 : "Cannot resolve the name to a(n) ' type definition' component.“