vonludi
vonludi

Reputation: 429

XSD for complex XML structure

I have a rather complex XML structure I am trying to validate and I can't seem to come up with a XSD structure that would allow me to express the following:

<foo fooAttribute1="..." fooAttribute2="..." ...>
  <bar1 id="1" ... />
  <bar1 id="2" ... />
  <bar2 id="1" ... />
  <bar2 id="2" ... />
  <![MyFormattedTextGoesHere[foo text goes here]]>
</foo>

So, I want to have a foo which can contain

On a related note: can I also validate attribute values like so: <xml someAttribute=$... /> (has to start with a $)?

What I currently have is

<xs:element name="foo" minOccurs="0" maxOccurs="unbounded">
  <xs:complexType mixed="true">
    <xs:sequence>
      <xs:element name="bar1" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="id" form="unqualified" type="xs:string" />
          <xs:attribute name="..." form="unqualified" type="xs:string" />
        </xs:complexType>
      </xs:element>
      <xs:element name="bar2" minOccurs="0" maxOccurs="unbounded">
        <xs:complexType>
          <xs:attribute name="id" form="unqualified" type="xs:string" />
          <xs:attribute name="..." form="unqualified" type="xs:string" />
        </xs:complexType>
      </xs:element>
    </xs:sequence>
    <xs:attribute name="fooAttribute1" form="unqualified" type="xs:string"/>
    <xs:attribute name="fooAttribute2" form="unqualified" type="xs:string"/>
    <xs:attribute name="..." form="unqualified" type="xs:string" />
    <!-- accept/validate text here? -->
  </xs:complexType>
  <!-- or here? -->
</xs:element>

Upvotes: 1

Views: 281

Answers (1)

Ghislain Fourny
Ghislain Fourny

Reputation: 7279

The XML above is not well-formed because of <! followed by general text. What is meant here may be a CDATA section, like so:

<?xml version="1.0" encoding="UTF-8"?>
<foo fooAttribute1="$..." fooAttribute2="..." >
   <bar1 id="1" />
   <bar1 id="2" />
   <bar2 id="1" />
   <bar2 id="2" />
   <![CDATA[MyFormattedTextGoesHere[foo text goes here]]>
</foo>

A good starting point for a schema against which the above XML is valid is this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:simpleType name="beginswithdollar">
        <xs:restriction base="xs:string">
            <xs:pattern value="\$.*"/>
        </xs:restriction>
    </xs:simpleType>
    <xs:element name="foo">
        <xs:complexType mixed="true">
            <xs:sequence>
                <xs:element name="bar1" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence/>
                        <xs:attribute name="id" type="xs:string"/>
                    </xs:complexType>
                </xs:element>
                <xs:element name="bar2"  minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence/>
                        <xs:attribute name="id" type="xs:string"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
            <xs:attribute name="fooAttribute1" type="beginswithdollar"/>
            <xs:anyAttribute processContents="lax"/>
        </xs:complexType>
    </xs:element>
</xs:schema>

A few things are not supported by XML Schema though:

  • Whether text is in CDATA sections or not is not visible by XML Schema. CDATA sections are used to avoid escaping special characters.
  • Complex types can be mixed-content or not. If mixed-content, it can not be controlled where the text appears or what its type is: it could also appear before or between bar* elements.
  • Attributes can be allowed with no restriction with xs:anyAttribute, but it is not possible to restrict their type in general. In the above schema, for example, the attribute fooAttribute1 must start with a dollar, while any other attributes are allowed with no restriction.

If XML Schema 1.1 is supported, there is also the assert feature that allows to express user-defined constraints. This may be a way to further restrict validity of instances in a tailored way beyond what other XML Schema components can do.

Upvotes: 1

Related Questions