Question

I am trying to grasp the technical side of working with the default namespace, when starting with a schema (developing one) and considering a realistic and correct corresponding XML file.

I am using Oxygen to design the schema.

Ideally, I would like that the user of the XML files do not have to deal with the default namespace (because they are painful, when trying to use XPath on documents which have declared the default namespace). However, from what I read so far, it seems that is not possible?

My example schema looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.com/" 
    xmlns="http://www.example.com/">
    <xs:element name="QueryResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Result"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Result">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Patient"/>
            </xs:sequence>
            <xs:attribute name="type" type="xs:NCName"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="VisitNumber" type="xs:string"/>
    <xs:element name="Demographics">
        <xs:complexType>
            <xs:sequence>
                <xs:element minOccurs="1" maxOccurs="1" name="firstNames" type="xs:string"/>
                <xs:element minOccurs="1" maxOccurs="1" name="surname" type="xs:string"/>
                <xs:element minOccurs="1" maxOccurs="1" name="dateOfBirth" type="xs:dateTime"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Patient">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="Demographics"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

and oxygen generates an example XML which looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<QueryResponse xmlns="http://www.example.com/"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.example.com/ file:/home/me/simplifiedResponse.xsd">
    <Result>
        <Patient>
            <Demographics>
                <firstNames xmlns="">firstNames0</firstNames>
                <surname xmlns="">surname0</surname>
                <dateOfBirth xmlns="">2006-05-04T18:13:51.0Z</dateOfBirth>
            </Demographics>
        </Patient>
    </Result>
</QueryResponse>

My question is:

  1. Why does Oxygen generate child elements with empty xmlns attributes ?
  2. How can I BEST avoid this?
  3. Is it possible to create a schema which can correctly describe and validate an XML document which, rather, has no declared namespace (default or otherwise).
  4. If the answer to (c) is, 'yes', how would you modify my schema example
  5. What is a really good document to explain all this in a bit more depth, but be readable at the same time (i.e., I don't find documents at http://www.w3.org/XML/ at all easy to understand or read).
Was it helpful?

Solution

  1. Because the elements in question are not in any namespace. They cannot use an alternative prefix, because unqualified expanded names can only be represented by unprefixed names. But they are children of elements using a non-empty default namespace. So the only way they can appear in the document is by clearing the default namespace declaration.

  2. Stop declaring the elements as not being in any namespace. The simplest approach is to add elementFormDefault="qualified" to the schema element.

  3. Yes.

  4. Omit the targetNamespace attribute on xs:schema.

  5. Google is your friend.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top