Dave E
Dave E

Reputation: 123

XML schema is resulting in an xml element without a namespace

I'm having some trouble with a very simple schema. In the xml body that the schema specifies the first child of the root element is specified as having xmlns="" which is causing validation problems for me.

I've searched and searched and tried repeatedly to figure out why it's happening and I've not had any success. This is causing a larger problem with my server code as I'm composing an xml body and validation of it is failing due to my element not having xmlns="".

I would like to understand why the xmlns="" is resulting from the schema definition and how to fix this.

Here is the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org" 
    xmlns="http://www.example.org">

    <xs:simpleType name="XYZ">
        <xs:restriction base="xs:string">
            <xs:maxLength value="10"/>
        </xs:restriction>
    </xs:simpleType>

    <xs:complexType name="ABC">
        <xs:sequence>
            <xs:element name="PQR" type="XYZ"/>
        </xs:sequence>
    </xs:complexType>

    <xs:element name="A1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="result" type="ABC"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Based off of this schema, the following document is created:

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result xmlns="">
        <PQR></PQR>
    </result>
</A1>

I would like this:

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result>
        <PQR></PQR>
    </result>
</A1>

I've been looking at this and trying things for about a day now. Shouldn't the <result> element just be part of the targetnamespace making xmlns="" unnecessary?

Additionally when I try to validate it I'm getting the following error:

System ID: /Users/dev/Desktop/Untitled3.xml
Main validation file: /Users/dev/Desktop/Untitled3.xml
Schema: /Users/dev/Desktop/test.xsd
Engine name: Xerces
Severity: error
Description: cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.example.org":PQR}'. One of '{PQR}' is expected.
Start location: 4:10
End location: 4:13
URL: http://www.w3.org/TR/xmlschema-1/#cvc-complex-type

Upvotes: 3

Views: 1764

Answers (1)

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

Adding

elementFormDefault="qualified"

to your xs:schema element should solve the problem. If I make this small modification and then let Oxygen generate a sample XML document, the result is

<?xml version="1.0" encoding="UTF-8"?>
<A1 xmlns="http://www.example.org">
    <result>
        <PQR>PQR0</PQR>
    </result>
</A1>

See e.g. this question and answers for an explanation why this helps, but the gist of it is: elementFormDefault="qualified" tells a validating processor that the elements you mention in your XSD are meant to be in the target namespace. The default for elementFormDefault is "unqualified".

If it is set to "unqualified", all elements will be assumed to be in no namespace by default, except for the outermost element of your XML document, which will be assumed to have the namespace you have specified here:

xmlns="http://www.example.org"

Upvotes: 3

Related Questions