0

My XML file consists of a general part, which I want to validate, and a special part, which is validated later (using a different xsd file which checks the general part again which is ok since it does not change).

How can I tell XMLSchema to check only for the element GeneralPart and not for the element SpecialPart? Also, the name SpecialPart should be interchangible if possible. So I'd like to replace <xs:element name="SpecialPart" minOccurs="0" maxOccurs="1"> with something like <xs:anything/>...

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


  <xs:element name="MyModule">
    <xs:complexType>
      <xs:sequence>

        <!-- General part -->
        <xs:element name="GeneralPart" minOccurs="0" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Version" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>

        <!-- Special part. -->
        <xs:element name="SpecialPart" minOccurs="0" maxOccurs="1">

      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Fabian
  • 4,001
  • 4
  • 28
  • 59

2 Answers2

2

There is an any element:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">


  <xs:element name="MyModule">
    <xs:complexType>
      <xs:sequence>

        <!-- General part -->
        <xs:element name="GeneralPart" minOccurs="0" maxOccurs="1">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Version" type="xs:string"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>

        <xs:any minOccurs="0" maxOccurs="1" namespace="##any" processContents="skip"/>

      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • This does not work for me. It seems that it makes the xsd file invalid. – Fabian Jun 26 '15 at 10:29
  • @Fabian - what message do you get? – Damien_The_Unbeliever Jun 26 '15 at 10:33
  • I use libxml++ and call `bool xmlpp:SchemaValidator::validate(const xmlpp::string& file)` which returns false. I don't know how to get more information. – Fabian Jun 26 '15 at 11:01
  • `` works while `xs:any minOccurs="0" maxOccurs="1" namespace="##any"/>` does not. Thank you very much. Can you explain what namespace and processContents mean or where I can find information about these attributes? – Fabian Jun 26 '15 at 11:06
  • 1
    @Fabian - I've linked to some documentation at the top of my answer. The `namespace` one isn't strictly necessary since its the default. – Damien_The_Unbeliever Jun 26 '15 at 11:08
0

Replacing <xs:element name="SpecialPart" minOccurs="0" maxOccurs="1"> with

<xs:element name="SpecialPart" type="xs:anyType" minOccurs="0" maxOccurs="1" />

works. However, I have to keep the name "SpecialPart".

Fabian
  • 4,001
  • 4
  • 28
  • 59