1

I have defined following schema for XML

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://www.learnjava.com"
                    xmlns="http://www.learnjava.com"
                    elementFormDefault="qualified"
                    attributeFormDefault="qualified">

    <!-- simple elements -->
    <xs:element name="name" type="xs:string"/>
    <xs:element name="hod" type="xs:string"/>
    <xs:element name="dept" type="xs:integer"/>

    <!-- attributes -->
    <xs:attribute name="id" type="xs:integer"/>

    <!-- complex elements -->
    <xs:element name="department">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="name"/>
                <xs:element ref="hod" minOccurs="0"/>
            </xs:sequence>
            <xs:attribute ref="id" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="departments">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="department"  maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="student">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="name"/>
                <xs:element ref="dept"/>
            </xs:sequence>
            <xs:attribute ref="id" use="required"/>
        </xs:complexType>
    </xs:element>
    <xs:element name="students">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="student"  maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="school">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="name"/>
                <xs:element ref="departments"/>
                <xs:element ref="students"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

As per the schema, the attribute id is required for <department> and <student> tags and I have below XML which adheres to this rule

<?xml version="1.0" encoding="UTF-8"?>
<school xmlns="http://www.learnjava.com"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
    <name>TAMUC</name>
    <departments>
        <department id="1001">
            <name>Computer Science</name>
        </department>
        <department id="1002">
            <name>Social Science</name>
            <hod>Jeff</hod>
        </department>
    </departments>
    <students>
        <student id="5001">
            <name>Frank</name>
            <dept>1001</dept>
        </student>
        <student id="5002">
            <name>Paul</name>
            <dept>1001</dept>
        </student>
    </students>
</school>

But the validation fails with below error

Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.3.2.2: Attribute 'id' is not allowed to appear in element 'department'.
Error - Line 7, 25: org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 25; cvc-complex-type.4: Attribute 'id' must appear on element 'department'.
[..further errors omited...]

Not sure for what is wrong. Both the error messages are contradictory

The validation fails if I remove "id" attributes from and as well as when I have it

I have a work around to make this work, by modifying the XSD like the one below

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://www.learnjava.com"
                    xmlns="http://www.learnjava.com"
                    elementFormDefault="qualified">
    <xs:element name="school">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="departments">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="department" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="name" type="xs:string"/>
                                        <xs:element name="hod" type="xs:string" minOccurs="0"/>
                                    </xs:sequence>
                                    <xs:attribute name="id" type="xs:integer"/>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="students">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="student" maxOccurs="unbounded">
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="name" type="xs:string"/>
                                        <xs:element name="dept" type="xs:integer"/>
                                    </xs:sequence>
                                    <xs:attribute name="id" type="xs:integer"/>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

but as an XML novice, I am curious to know what is wrong in the earlier XSD

PS:
1. I'm using http://www.utilities-online.info/xsdvalidation/#.VYpP8vmqqko to validate XML against the XSD.
2. I observe the same error in Altova XML Spy editor as well.

1 Answer 1

1

the problem is that the attribute id has been declared global(direct child of xs:schema). therefore, the attribute id should be qualified with the namespaces http://www.learnjava.com. Notice that id is not the same thing as http://www.learnjava.com:id If you don't want that the attribute id belong to the namespace:

  1. you should use a local attribute declaration
  2. In your schema document, change

     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://www.learnjava.com"
                    xmlns="http://www.learnjava.com"
                    elementFormDefault="qualified"
                    attributeFormDefault="qualified">
    

to

     <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                    targetNamespace="http://www.learnjava.com"
                    xmlns="http://www.learnjava.com"
                    elementFormDefault="qualified"
                   >

Update:

If you want to use global attribute declaration, then your xml instance document would look like the following:

<p:school xmlns:p="http://www.learnjava.com"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.learnjava.com ex2.xsd">
    <p:name>TAMUC</p:name>
    <p:departments>
        <p:department p:id="1001">
            <p:name>Computer Science</p:name>

        </p:department>
        <p:department p:id="1002">
            <p:name>Social Science</p:name>
            <p:hod>Jeff</p:hod>
        </p:department>
    </p:departments>
    <p:students>
        <p:student p:id="5001">
            <p:name>Frank</p:name>
            <p:dept>1001</p:dept>
        </p:student>
        <p:student p:id="5002">
            <p:name>Paul</p:name>
            <p:dept>1001</p:dept>
        </p:student>
    </p:students>
</p:school>
Sign up to request clarification or add additional context in comments.

5 Comments

I want to reuse id attribute declaration across <department> and <student> tags. So, want to make it global. I tried removing attributeFormDefault as suggested by you and not making attribute declaration local, no luck!!
make global attribute declaration, it means that it must be qualified in the xml instance document. In your xml doc, you should use a namespaces with prefix .This attribute must be qualified with that namespaces with prefix .
I applied your suggestion and it works fine now. I have couple of questions. 1. why is it that I need to qualify the attribute with a namespace prefix? 2. why only attribute? why not other element's? (even the elements are declared global)
If you used targetNamespace, Each nodes (elements, attributes ) that you declare globally must be qualified by a namespace. 1. because the default namespace does not apply to attribute names. 2. No. these elements are qualified using default namespace (A default namespace declaration applies to all unprefixed element names within its scope.).
Thanks @Win.ubuntu. Its a learning for me. I was not aware that default namespace does not apply to attribute names. My questions are answered, thanks for your contribution to this community!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.