user3083431
user3083431

Reputation: 1

XSD XML - validation error while using restriction

I have two valid XSD files (a.xsd and b.xsd). I try to validated a XML file (example.xml) against the Schema and get an error.

Can anybody explain to me, why I get a validation error?

THX


a.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://namespace/a" xmlns:a="http://namespace/a" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="A">
    <xs:sequence>
      <xs:element name="E1" type="xs:token" form="unqualified"/>
      <xs:element name="E2" type="xs:token" form="unqualified" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

b.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://namespace/b" xmlns:b="http://namespace/b" xmlns:a="http://namespace/a" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://namespace/a" schemaLocation="a.xsd"/>
  <xs:element name="START" type="b:B"/>
  <xs:complexType name="B">
    <xs:complexContent>
      <xs:restriction base="a:A">
        <xs:sequence>
          <xs:element name="E1">
            <xs:simpleType>
              <xs:restriction base="xs:token">
                <xs:enumeration value="value1">
                  <xs:annotation>
                    <xs:appinfo>
                      <codeName>value1</codeName>
                    </xs:appinfo>
                  </xs:annotation>
                </xs:enumeration>
                <xs:enumeration value="value2">
                  <xs:annotation>
                    <xs:appinfo>
                      <codeName>value2</codeName>
                    </xs:appinfo>
                  </xs:annotation>
                </xs:enumeration>
                <xs:enumeration value="value3">
                  <xs:annotation>
                    <xs:appinfo>
                      <codeName>value3</codeName>
                    </xs:appinfo>
                  </xs:annotation>
                </xs:enumeration>
              </xs:restriction>
            </xs:simpleType>
          </xs:element>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>

example.xml

<?xml version="1.0" encoding="UTF-8"?>
<b:START xmlns="http://namespace/a" xmlns:b="http://namespace/b" xsi:schemaLocation="http://namespace/b C:\problem\b.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <E1>value1</E1>
</b:START>

Upvotes: 0

Views: 305

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21658

It is the declaration of xmlns="http://namespace/a" in your sample XML which throws your XML off... And it is because your a.xsd uses the form="unqualified"/ attribute to define E1 and E2.

The effect of this attribute is that it denies a namespace to your element, which is to say these elements must have no namespace - they're not qualified.

To fix your XML you have two options, based on your sample:

Remove the xmlns="http://namespace/a" from your root element:

<?xml version="1.0" encoding="UTF-8"?>
<b:START xmlns:b="http://namespace/b" xsi:schemaLocation="http://namespace/b b.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <E1>value1</E1>
</b:START>

OR, add xmlns="" to your E1 element:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<START xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://namespace/a" xmlns="http://namespace/b">
    <E1 xmlns="">value1</E1>
</START>

There might be other variants, but in the end, the above are essential to understand how to eventually override at the element level a namespace (scoping, basically).

Upvotes: 2

Related Questions