Question

The following is my XML and its associated XSD:

XML

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

<people xmlns:xsi="http://www.w3c.org/200/10/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="student.xsd">
    <student>
        <name>John</name>
        <course>Computer Technology</course>
        <semester>6</semester>
        <scheme>E</scheme>
    </student>

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

XSD

<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <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>  

What I now want to do is to add a namespace - a hypothetical university to which the students belong - say, Carnegie Mellon.

I know how to add namespaces to an XML document. That would be as follows:
xmlns:cmu = "http://www.carnegiemellon.com/ns/students"
and there would be associated prefixes in the XML.

What I want to know is: How do I validate the XML with prefixes using the XSD?

Was it helpful?

Solution

My answer here should answer your question... Still, you have to consider the following:

Are the people and student in the same namespace? if yes, then just add a targetNamespace with the uri that you mentioned. Otherwise, you have to add an xsd:import, and create a new XSD with the targetNamespace that you want which defines the student.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top