3
votes

I'm having a problem with my XSD where I am getting this error when I enter my code into a XSD validator: "no-xmlns: The {name} of an attribute declaration must not match 'xmlns'."

Here is my XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Test">
        <xs:complexType>
            <xs:attribute name="xmlns" type="xs:string" use="required"/>
            <xs:sequence>
                <xs:choice minOccurs="1" maxOccurs="unbounded">
                    <xs:element name="Screens" minOccurs="0" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="Screen" minOccurs="1" maxOccurs="unbounded">
                                    <xs:complexType>
                                        <xs:attribute name="name" type="xs:string" use="required"/>
                                        <xs:attribute name="package" type="xs:string" use="required"/>
                                        <xs:attribute name="class" type="xs:string" use="required"/>
                                        <xs:sequence>
                                            <xs:element name="ScreenData" minOccurs="1" maxOccurs="unbounded">
                                                <xs:complexType>
                                                    <xs:attribute name="step" type="xs:int" use="required"/>
                                                    <xs:attribute name="description" type="xs:string" use="required"/>
                                                    <xs:sequence>
                                                        <xs:element name="element" minOccurs="1" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:attribute name="name" type="xs:string" use="required"/>
                                                                <xs:attribute name="type" type="xs:string" use="required"/>
                                                                <xs:attribute name="value" type="xs:string" use="required"/>
                                                            </xs:complexType>
                                                        </xs:element>
                                                    </xs:sequence>
                                                </xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="DBSession" minOccurs="0" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="use_test.properties" type="xs:boolean" use="required"/>
                            <xs:attribute name="use_dbserver" type="xs:string" use="required"/>
                            <xs:attribute name="use_db" type="xs:string" use="required"/>
                            <xs:sequence>
                                <xs:element name="login" minOccurs="1" maxOccurs="1">
                                    <xs:complexType>
                                        <xs:attribute name="server" type="xs:string" use="required"/>
                                        <xs:attribute name="database" type="xs:string" use="required"/>
                                        <xs:attribute name="username" type="xs:string" use="required"/>
                                        <xs:attribute name="password" type="xs:string" use="required"/>
                                        <xs:sequence>
                                            <xs:element name="Queries" minOccurs="1" maxOccurs="1">
                                                <xs:complexType>
                                                    <xs:sequence>
                                                        <xs:attribute name="query" type="xs:string" use="required"/>
                                                        <xs:attribute name="expectedResults" type="xs:string" use="required"/>
                                                    </xs:sequence>
                                                </xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="CLISession" minOccurs="0" maxOccurs="unbounded">
                        <xs:complexType>
                            <xs:attribute name="use_test.properties" type="xs:boolean" use="required"/>
                            <xs:sequence>
                                <xs:element name="login" minOccurs="1" maxOccurs="unbounded">
                                    <xs:complexType>
                                        <xs:attribute name="host" type="xs:string" use="required"/>
                                        <xs:attribute name="password" type="xs:string" use="required"/>
                                        <xs:attribute name="username" type="xs:string" use="required"/>
                                        <xs:sequence>
                                            <xs:element name="Commands" minOccurs="1" maxOccurs="1">
                                                <xs:complexType>
                                                    <xs:sequence>
                                                        <xs:element name="Command" minOccurs="1" maxOccurs="unbounded">
                                                            <xs:complexType>
                                                                <xs:attribute name="exe" type="xs:string" use="required"/>
                                                                <xs:attribute name="args" type="xs:string" use="required"/>
                                                                <xs:attribute name="wait" type="xs:int" use="required"/>
                                                                <xs:attribute name="expectedOutput" type="xs:string" use="required"/>
                                                                <xs:attribute name="toVariable" type="xs:string" use="required"/>
                                                            </xs:complexType>
                                                        </xs:element>
                                                    </xs:sequence>
                                                </xs:complexType>
                                            </xs:element>
                                        </xs:sequence>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

I am assuming it is something to do with the second line of code in the schema. This line is related to the following line in my XML document:

<Test xmlns="http://MyURL">
1
I believe it is saying the name of your first attribute cannot be xmlnsthatidiotguy

1 Answers

5
votes

The problem is in your schema. Just delete the line:

<xs:attribute name="xmlns" type="xs:string" use="required"/>

The XML document is correct. In your document, you chose to bind the URI http://MyURL to the default namespace by assigning that value attribute xmlns. This is perfectly fine, as explained in Namespaces in XML 1.0.

Namespaces are special. See the last paragraph of XML Schema Part 1: 3.2.1 Attribute Details

[XML-Infoset] distinguishes attributes with names such as xmlns or xmlns:xsl from ordinary attributes, identifying them as [namespace attributes]. Accordingly, it is unnecessary and in fact not possible for schemas to contain attribute declarations corresponding to such namespace declarations, see xmlns Not Allowed (§3.2.6). No means is provided in this specification to supply a default value for a namespace declaration.

All the content in the schema you have written is in the namespace you declare it in, so it isn't really necessary to tie down document writers to declaring it exactly that way. They could declare the namespace on a higher-level element. Or they could bind it to a different namespace prefix.

Incidentally, you should probably declare your schema's namespace in the document by setting targetNamespace on your topmost schema element as explained in the XML Schema Primer

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://MyURL">