0
votes

Is there a simple XML schema (XSD) to allow any combination of nested tags (i.e literally any element names) but no mixed tags + [non-tagged]text?

So this would be invalid:

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

But this would be fine:

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

Restated: All content must be between a matched tag pair.

1

1 Answers

1
votes

Yes, xs:any will allow any elements beneath root and will not allow mixed content:

<?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>

If you also wanted to allow mixed content, you would add mixed="true" to xs:complexType.

See also processContents strict vs lax vs skip for xsd:any.


Update to address comment:

How about the child nodes of those directly under the ? And all further children down the tree? The default mixed="false" would apply to those too?

No, the above XSD wouldn't prevent mixed content in children of root.

To prevent mixed content deeper in the tree, you could use `processContents="strict" and preclude mixed content in the allowed elements. If this is too restrictive, in XSD 1.1 you could use an assertion:

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>