Dimitri De Franciscis
Dimitri De Franciscis

Reputation: 1032

XML Schema validation with empty namespace

I tried to understand follow all the (complicated) rules of XML Schema handling, but still I cannot figure this out! My problem is this:

However, all validating editors give me the same error message.

Here is a stripped-down version of the XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema targetNamespace="http://www.megadix.org/standards/temp.xsd" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:jfcm="http://www.megadix.org/standards/temp.xsd"
elementFormDefault="unqualified" attributeFormDefault="unqualified">

<xsd:complexType name="MapsType">
    <xsd:sequence>
        <xsd:element name="map" minOccurs="0" maxOccurs="unbounded">
            <xsd:complexType>
                <xsd:attribute name="name" type="xsd:string" use="required"></xsd:attribute>
                <xsd:sequence>
                    <xsd:element name="description" type="xsd:string" minOccurs="0" maxOccurs="1"></xsd:element>
                    <xsd:element name="concepts" minOccurs="0" maxOccurs="1">
                        <xsd:complexType>
                            <xsd:sequence>
                                <xsd:element name="concept" minOccurs="0" maxOccurs="unbounded">
                                    <xsd:complexType>
                                        <xsd:attribute name="name" type="xsd:string" use="required"></xsd:attribute>
                                    </xsd:complexType>
                                </xsd:element>
                            </xsd:sequence>
                        </xsd:complexType>
                    </xsd:element>
                </xsd:sequence>
            </xsd:complexType>
        </xsd:element>
    </xsd:sequence>
</xsd:complexType>

<xsd:element name="maps" type="jfcm:MapsType"></xsd:element>

If I use a prefix, files correctly validate and auto-complete:

<?xml version="1.0" encoding="UTF-8"?>
<jfcm:maps xmlns:jfcm="http://www.megadix.org/standards/temp.xsd">
<map name="Test Map">
    <concepts>
        <concept name="c1" />
        <concept name="c2" />
        <concept name="c3" />
    </concepts>
</map>

But if I remove it:

<?xml version="1.0" encoding="UTF-8"?>
<maps xmlns:jfcm="http://www.megadix.org/standards/temp.xsd">
<map name="Test Map">
    <concepts>
        <concept name="c1" />
        <concept name="c2" />
        <concept name="c3" />
    </concepts>
</map>

I get this error message:

Invalid content was found starting with element 'map'. One of '{map}' is expected.

Any suggestions?

Upvotes: 1

Views: 2385

Answers (3)

Michael Kay
Michael Kay

Reputation: 163468

Remember that prefixes are irrelevant. It's the namespace that counts.

If you want the "maps" element to be in no namespace, then you must declare it that way in the schema. Currently you have declared it as a global element declaration in a schema document with a target namespace.

Your requirement seems very strange. Your instance document declares a namespace, but then it doesn't use that namespace for any of the elements in the document. That's legal, but it's a very odd thing to want to do.

Upvotes: 0

Phani
Phani

Reputation: 5427

You can understand lot about namespaces and their relationship in terms of validation from here.

XML Schema Validation

Upvotes: 0

Petru Gardea
Petru Gardea

Reputation: 21658

Since you're using "unqualified" for element's form, this is the correct way of doing it:

<?xml version="1.0" encoding="UTF-8"?>
<maps xmlns="http://www.megadix.org/standards/temp.xsd">
  <map name="Test Map" xmlns="">
    <concepts>
      <concept name="c1" />
      <concept name="c2" />
      <concept name="c3" />
    </concepts>
  </map>
</maps>

If you want all elements to "share" on the namespace, you have to define elementFormDefault="qualified"; as a sidebar, many XSD designers would consider this as a good advice.

Upvotes: 3

Related Questions