【问题标题】:XML schema to allow any combination of nested tags but no mixed tags + [non-tagged]text?XML模式允许嵌套标签的任意组合,但不允许混合标签+ [非标签]文本?
【发布时间】:2016-12-06 17:52:48
【问题描述】:

是否有一个简单的 XML 模式 (XSD) 允许嵌套标签的任意组合(即任何元素名称)但不允许混合标签 + [非标签] 文本?

所以这将是无效的:

<?xml version="1.0" encoding="UTF-8"?> <root>Some text <tag1>Other text</tag1></root>

不过这样就好了:

<root><tag2>Some text</tag2> <tag1>Other text</tag1> <tag1>Third text<tag2>Last text</tag2></tag1></root>

重述:所有内容必须在匹配的标记对之间。

【问题讨论】:

    标签: xml xsd


    【解决方案1】:

    是的,xs:any 将允许root 下的任何元素,并且不允许混合内容:

    <?xml version="1.0" encoding="UTF-8"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="root">
        <xs:complexType>
          <xs:sequence>
            <xs:any namespace="##any" processContents="lax"
                    minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    如果您还想允许混合内容,请将mixed="true" 添加到xs:complexType

    另见processContents strict vs lax vs skip for xsd:any


    更新地址评论:

    那些直接下的子节点呢?和所有 更多的孩子在树下?默认的混合=“假”将适用 也给那些?

    不,上述 XSD 不会阻止 root 的子项中的混合内容。

    为了防止树中更深的混合内容,您可以使用 `processContents="strict" 并在允许的元素中排除混合内容。如果这限制性太强,在 XSD 1.1 中您可以使用断言:

    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="root">
        <xs:complexType>
          <xs:sequence>
            <xs:any namespace="##any" processContents="lax"
                    minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
          <xs:assert test="every $e in .//* 
                           satisfies not($e/* and $e/text()[normalize-space()])"/>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    

    【讨论】:

    • &lt;root&gt;下的子节点呢?还有树下的所有孩子?默认的mixed="false" 也适用于那些?
    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 2011-04-18
    • 2016-07-02
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-01-27
    相关资源
    最近更新 更多