0

I am trying to validate my XML document using an XSD. I have tried providing namespaces to everything, including the default. However, the error persists. if someone could please tell me what is going wrong then it will be highly appreciated

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- <!DOCTYPE people SYSTEM "validator.dtd"> -->

    <people 
    xmlns:cmuq="http://www.cmu.edu/ns/students"
    xmlns="http://www.cmu.edy/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="student.xsd"
    >
        <cmuq:student>
            <name>John</name>
            <course>Computer Technology</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </cmuq:student>

        <cmuq:student>
            <name>Foo</name>
            <course>Industrial Electronics</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </cmuq:student>
    </people>   

XSD

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.cmu.edu/ns/blank"
targetNamespace="http://www.cmu.edu/ns/students">
    <xs:element name="people">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="student" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="name" type="xs:string" />
                            <xs:element name="course" type="xs:string" />
                            <xs:element name="semester">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:enumeration value="1" />
                                        <xs:enumeration value="2" />
                                        <xs:enumeration value="3" />
                                        <xs:enumeration value="4" />
                                        <xs:enumeration value="5" />
                                        <xs:enumeration value="6" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            <xs:element name="scheme">
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value = "E|C" />
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>  

Please tell me how to solve that error


Update after Ian Roberts' answer

The XSD <schema> tag:

  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns="http://www.cmu.edu/ns/blank"
    targetNamespace="http://www.cmu.edu/ns/blank"
    elementFormDefault="qualified"> 

The root of the XML, <people> tag.

 <people
    xmlns="http://www.cmu.edu/ns/blank"
    xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.cmu.edu/ns/blank student.xsd">  

It still does not validate. I have, for now, dropped the idea of adding xmlns:cmuq but the problem persists. >

An SO User
  • 24,612
  • 35
  • 133
  • 221

1 Answers1

1

The schema as you have it there declares the people element in the http://www.cmu.edu/ns/students namespace (the target namespace of the schema) and all the nested elements in no namespace (because you don't use elementFormDefault). So it needs to be

<cmuq:people
  xmlns:cmuq="http://www.cmu.edu/ns/students"
  xmlns:xsi="http://www.w3c.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.cmu.edu/ns/students student.xsd"
>
    <student>

Note also that the xsi:schemaLocation needs to be a list of namespaceUri schema pairs, not just a single schema address - this attribute can associate a different schema with each namespace.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Basically, what I was attempting to do is to add hypothetical students of `Cornell`, `CMU` and other univerisities and then validate them using various XSDs. How do I do thta ? – An SO User May 10 '13 at 08:25
  • @LittleChild I suggest you make that a separate question, because the answer will be quite involved, using derived types and element substitution groups (look this up in the xmlschema-0 primer at w3.org and you may be able to work it out for yourself). – Ian Roberts May 10 '13 at 08:40
  • 1
    @LittleChild with that edit the schema now expects all the elements to be in the `http://www.cmu.edu/ns/blank` namespace, so with that `xmlns=` on the `people` element, its children need to be just `` (inheriting the default namespace), not `` – Ian Roberts May 10 '13 at 11:24
  • Have a look: http://stackoverflow.com/questions/16481870/what-does-targetnamespace-do-am-i-getting-it-right – An SO User May 10 '13 at 12:07