0

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).
Shon
  • 690
  • 6
  • 22
svaens
  • 649
  • 7
  • 23

1 Answers1

2
  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.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
  • Hi, thanks for the answer. I'd been playing and came across a suggestion to use the 'elementFormDefault="qualified", which did get rid of the empty xmlns attributes. But I was left with, of course, either putting all the resulting xml elements in the default namespace, or in a real namespace. So, towards achieving the simplest document, I wanted to forge ahead and try your suggestion on point 4. This resulted in errors from oxygen. Description: src-resolve.4.2: Error resolving component 'Result'. It was detected that 'Result' is in namespace... blah blah. – svaens Jun 04 '13 at 16:31
  • ahh .. I had to put the whole thing in a non-default namespace. Ok, trial and error, but it makes no sense to me yet why that was necessary. Have to find that elusive needle in a haystack tutorial which clarifies it... google is not always my friend. Would prefer a good thorough book. – svaens Jun 04 '13 at 16:41