Question

I have a question about XSD?

In dtd, we can use any to not restrict the element type and tag like

<!ELEMENT question ANY>
<question>
  <q1>question 1</q1>
</question>

Is there a way to do it in XSD?

Was it helpful?

Solution

If you want to allow any elements inside question you can use <xs:any> which represents any element:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="question">
    <xs:complexType>
      <xs:sequence>
        <xs:any minOccurs="0" maxOccurs="unbounded" processContents="skip"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

The attribute processContents="skip" is used to inform the parser to skip validation of the child elements. You might want to use lax instead of skip if you are including tags that might be validated if a schema is available, such as XHTML tags. The value strict requires that the tags that eventually are used in your instance be declared in a Schema.

If you also want to allow text to appear outside elements such as in:

<question>
  <q1>question 1</q1>
  some text
</question>

Then add mixed="true" as an attribute to <complexType>.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top