Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jozef (8299)

jozef
  (email not shown publicly)
http://jozef.kutej.net/
Jabber: jozef@kutej.net

Journal of jozef (8299)

Monday February 09, 2009
06:26 AM

unqualified XML schema

[ #38433 ]

Last week I spent significant amount of time troubleshooting SOAP messages, checking XML messages and reading WSDL and XSD files.

The outcome is to always note this little option switch in XSD root element called - elementFormDefault="qualified". If not set this option defaults to unqualified. unqualified is only good and should be used when we need the XMLs with empty namespaces. When there starts to be namespaces always set elementFormDefault="qualified". This will ensure that all elements has their proper namespace.

Some examples to explain:

Imagine we want to have a schema for following XML:

<note>
        <text>huh?</text>
</note>

Then the XSD will look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
        version="1.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="note" type="noteText" />
<xs:complexType name="noteText">
        <xs:sequence>
                <xs:element name="text" type="xs:string" minOccurs="0"/>
        </xs:sequence>
</xs:complexType>
</xs:schema>

So far so "unqualified" good. When xs:schema root element get's parameter called targetNamespace="http://justns/" then the valid XML looks a bit different:

<ns1:note xmlns:ns1="http://justns/">
        <text>huh?</text>
</ns1:note>

Note that "note" element has a namespace and "text" element has an empty namespace. And this was cause of my troubles. First thing XML::Compile needs an extra option "elements_qualified => 'TOP'" to work properly with this kind of mix. The second problem starts when encapsulating this XML in other XML like SOAP envelope. If the envelope sets an default "xmlns" then all the elements without namespace inherits the default one which is wrong.

Mark Overmeer say: "Unqualified is out of fashion: has no future.". He is right. Only for small XMLs where there will NEVER EVER be any namespace, the unqualified is useful.

Just for a completenes the example XML+XSD with qualified turned on:

<ns1:note xmlns:ns1="http://justns/">
        <ns1:text>huh?</ns1:text>
</ns1:note>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema
        version="1.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:tns="http://justns/"
        targetNamespace="http://justns/"
        elementFormDefault="qualified"
>

<xs:element name="note" type="tns:noteText" />

<xs:complexType name="noteText">
        <xs:sequence>
                <xs:element name="text" type="xs:string" minOccurs="0"/>
        </xs:sequence>
</xs:complexType>

</xs:schema>

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • The way namespaces are implemented in XML are not too help full for us humans. An XML parser can keep track of the namespace of a node by checking the prefix used and going up in the DOM until it finds the namespace declaration or if there's no prefix by looking for the closest parent that defines a default namespace.

    For a human this is very error prone, specially if the XML file is generated by a program and that namespaces are redeclared everywhere. This worsens if the namespaces get associated to differe