tags:

views:

68

answers:

2

Hi,

I'm trying to create an xml-schema (xsd) to validate an xmlfile.

<a>
    <b>
     <c>...</c>
     <d>...</d>
    </b>
    <b>
     <c>...</c>
     <e>...</e>
            <d>...</d>
    </b>
<a>

1 a-element. Multiple b-elements, that have some content.

I want to validate that a is present in the file, and 1 or more occurances of b. I'm not interested to know what is inside of b.

So this is what I tried:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="a">
     <xs:complexType>
      <xs:sequence>
       <xs:element name="b" minOccurs="1" maxOccurs="unbounded"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
    <xs:element name="b">
     <xs:complexType>
      <xs:sequence>
       <xs:any minOccurs="1"/>
      </xs:sequence>
     </xs:complexType>
    </xs:element>
</xs:schema>

I hoped that the any-element would do the magic trick, but it does not. What am I doing wrong?

edit: XmlSpy gives me this error: Element 'c' not defined in DTD/Schema.

+2  A: 

You don't need the additional 'b' in the schema, I think what you're looking for is this:

    <?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="a">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="b" minOccurs="1" maxOccurs="unbounded">
          <xs:complexType>
            <xs:sequence>
              <xs:any maxOccurs="unbounded" minOccurs="1" processContents="lax"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

This states that you have <a> as a root node, and it can contain one or more <b>s

Edit: Sorry, didn't read the full question - try the above!

Second edit: Another attempt above!

Fiona Holder
Unfortunately not. XmlSpy gives me this error: Element 'c' not defined in DTD/Schema.
Natrium
after edit: still the same error: XmlSpy gives me this error: Element 'c' not defined in DTD/Schema. +1 for effort though.
Natrium
Pretty sure the above will work now :)
Fiona Holder
yep, it works now.
Natrium
+1  A: 

isn't as free and easy as first appears. By default, I believe, the contents of the element must still conform to the schema, it's just that they can be anything from that schema.

If you want to have elements not present in the schema, you need to define it as this:

<xs:any minOccurs="1" processContents="lax"/>

You can use "skip" rather than "lax", which is even less restrictive.

See the W3C spec for more info.

skaffman
XmlSpy gives me this error: Element 'c' not defined in DTD/Schema. Both with lax and skip.
Natrium
XmlSpy doesn't always get it right. It's buggy as hell.
skaffman