Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does targetNamespace do ? Am I getting it right?

Here is the <schema> tag of my XSD:

<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">  

If my understanding is correct, here is what it means:

  • This schema itself belongs to http://www.w3.org/2001/XMLSchema namespace
  • The root of the XML instance should belong to http://www.cmu.edu/ns/blank namespace
  • All the elements within the XML instance which do not have a prefix automatically belong to http://www.cmu.edu/ns/blank namespace as elementFormDefault is qualified
  • Question1: Is my understanding correct. If not, what is wrong ?

    Question2 Look at the undermentioned XML instance:

    <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"
    >
        <student>
            <name>John</name>
            <course>Computer Technology</course>
            <semester>6</semester>
            <scheme>E</scheme>
        </student>
    </people>  
    

    Here everything belongs to http://www.cmu.edu/ns/blank namespace including <student> and the elements contained within because of elementFormDefault. Correct?

    Question3
    Now, I want to add <student> from various Universities. with prefixes like berk for Berkley, harv for Harvard, etc. Each <student> has a different set of elements within. And I want to validate that. How is that possible?

    like image 688
    An SO User Avatar asked Jan 24 '26 01:01

    An SO User


    1 Answers

    (1) The first two points are OK; the third one:

    All the elements within the XML instance which do not have a prefix automatically belong to http://www.cmu.edu/ns/blank namespace as elementFormDefault is qualified

    is incorrect.

    Declaring a prefix in the schema doesn't mean that the XML instance must use the same prefixes. Any namespace declaration in the XSD file, applies only to the XML file that is XSD (XSD is an XML, therefore...)

    In general, there is no way to assume anything about any prefixed or un-prefixed element name; i.e. below examples are all correct.

    <some xmlns="" .../>
    <some xmlns="urn:tempuri-org:XSD:1" .../>
    <x:some xmlns:x="urn:tempuri-org:XSD:1" .../>
    

    The only sure thing is that the only way to represent an unqualified name is through a name without a prefix (i.e. one cannot prefix the "empty" namespace).

    elementFormDefault controls the form of the element's name, when an element is declared within a content model (i.e. is not global).

    (2) Partially correct. The part because of elementFormDefault. is incorrect. Again, XSD is just one schema spec; XML exists and has its own rules, irrespective of XSD, or any other schema language. The rule that applies here is that of XML namespaces, specifically scoping.

    (3) You would have to create an XSD for each namespace; within each namespace, you declare the student and it's content. Then the XSD which defines people would import the other XSDs and reference students appropriately.

    So this is a basic setup:

    Berkeley.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:berkeley-org" xmlns="urn:berkeley-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="student"/>   
    </xsd:schema>
    

    Harvard.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:harvard-org" xmlns="urn:harvard-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:element name="student"/>   
    </xsd:schema>
    

    people.xsd

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
    <xsd:schema targetNamespace="urn:people-org" xmlns="urn:people-org" xmlns:harv="urn:harvard-org" xmlns:berk="urn:berkeley-org" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <xsd:import namespace="urn:harvard-org"  schemaLocation="harvard.xsd"/>
        <xsd:import namespace="urn:berkeley-org" schemaLocation="berkeley.xsd"/>
    
        <xsd:element name="people">
            <xsd:complexType>
                <xsd:choice maxOccurs="unbounded">
                    <xsd:element ref="harv:student"/>
                    <xsd:element ref="berk:student"/>               
                </xsd:choice>
            </xsd:complexType>
        </xsd:element>
    </xsd:schema>
    

    The files graph:

    enter image description here

    A sample XML (shows the use of namespaces):

    <?xml version="1.0" encoding="utf-8" standalone="yes"?>
    <!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
    <people xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:harv="urn:harvard-org" xmlns:berk="urn:berkeley-org" xmlns="urn:people-org">
        <harv:student/>
        <berk:student/>
    </people>
    

    enter image description here

    like image 181
    Petru Gardea Avatar answered Jan 25 '26 19:01

    Petru Gardea



    Donate For Us

    If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!