简体   繁体   中英

XML XSD - Allowing arbitrary grand children elements

I want to create an XSD schema in which only the root element and its immediate child is policed by the XSD. In other words I want to allow arbitrary grand-children of the root element while root element and children elements are strictly validated by the XSD. So if I have

<text>
   <language>
    ...
   </language>
</text>

It should enforce the presence of text and language but impose no restriction what sort of XML tags I add below the level of .

Is that possible at all? I have used XSD but not to great extent and cannot find a reference, neither stating I can do it, nor that it is forbidden to do so.

Use xsd:any at the grandchild level:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0">
  <xs:element name="text">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="language">
          <xs:complexType>
            <xs:sequence>
              <xs:any/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>  
</xs:schema>

Other considerations:

  • Adjust the cardinality constraints ( minOccurs and maxOccurs ) per your needs.
  • Add mixed="true" for mixed content (text and elements) per your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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