superbobich
superbobich

Reputation: 27

How many different XML Namespaces are in this document?

Ok so I have this document:

    <?xml version="1.0" encoding="utf-8"?>
    <md:madcow xmlns:md="urn:Annotation"
               xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns ="urn:Annotation"
               kind="withContent" media="text">
        <md:metadata>
            <md:author>ferro697702212</md:author>
            <md:title> Eine weitere Annotation </md:title>
            <md:creationDate>18/11/2009 10.26.09</md:creationDate>
            <md:modificationDate>18/11/2015 10.26.09</md:modificationDate>    

            <md:sourceHttp>687474703a2f2f7777772e676f6f676c652e69742f</md:sourceHttp>
                <md:type>example</md:type>
                <md:public>true</md:public>
        </md:metadata>
        <annotationBody xmlns="urn:Annotation">
            <contents id="1">
                <textContent>Das ist ein Beispieltext für eine Annotation</textContent>
                <attachments>
                   <attachedImage> file:://A/B/C</attachedImage>
                </attachments>
            </contents>

            <contents id="2">
                <md:textContent> Eine weitere Annotation </md:textContent>
                <attachments>
                    <attachedAudio>http://www.h_da.de/xml/test.mp3</attachedAudio>
                </attachments>
            </contents>

            <textSelection>
                <path>BODY/CENTER/FORM/TABLE[2]/TBODY/TR[2]/TD/FONT/LABEL[3],23,6</path>
                <contentRef>1</contentRef>
            </textSelection>
        </annotationBody>
    </md:madcow>

And here is my question:

How many different XML Namespaces are in this document?

As far as I can see it there is first:

xmlns:md="urn:Annotation"

which means this is the first one.

Then I have:

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

which is the second one.

Then we have:

xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"

which tells us where the schema for the xsi namespace is defined.

Then we have:

xmlns="urn:Annotation"

which tells us what the default namespace is.

And last we have:

<annotationBody xmlns="urn:Annotation">

which tells us that here we overwrite the default namespace for this particular element. This is btw what confuses me most, because why the hell do we need the namespace declaration here since its the default namespace anyway?

So yeah...

Did I understand/read the code correctly and if not can you please tell my why and how many different namespaces are in this document?

Thanks to everybody in advance.

Upvotes: 0

Views: 112

Answers (2)

Michael Kay
Michael Kay

Reputation: 163360

How many different XML Namespaces are in this document?

None.

As @Damien_The_Unbeliever correctly points out, two distinct namespace URIs appear in the document: urn:Annotation and http://www.w3.org/2001/XMLSchema-instance. But these namespaces are not "in" the document, they are outside it. That's the whole point about namespaces, they aren't local to a document.

If I have counted correctly, the document contains 4 namespace declarations representing 3 distinct namespace bindings to 2 distinct namespaces.

Upvotes: 0

Damien_The_Unbeliever
Damien_The_Unbeliever

Reputation: 239724

There are two different namespaces in this document, which are urn:Annotation and http://www.w3.org/2001/XMLSchema-instance.

The urn:Annotation is mentioned three times, and it is both the default namespace for the document and is also associated with the md namespace prefix. Namespace prefixes are arbitrary and only have meaning within the elements in which they're defined.

This means that each of the following three elements are in the same namespace and carry exactly the same information content:

<Element xmlns="urn:example"/>
<a:Element xmlns:a="urn:example"/>
<b:Element xmlns:b="urn:example"/>

In your particular document, there is a certain amount of redundancy, in that the default namespace didn't need to be re-assigned in the annotationBody element. Nor did we really need the md prefix.

This document carries exactly the same information:

<?xml version="1.0" encoding="utf-8"?>
<madcow
           xsi:schemaLocation="urn:Annotation D:\projects\DELOS\Annotation.xsd"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns ="urn:Annotation"
           kind="withContent" media="text">
    <metadata>
        <author>ferro697702212</author>
        <title> Eine weitere Annotation </title>
        <creationDate>18/11/2009 10.26.09</creationDate>
        <modificationDate>18/11/2015 10.26.09</modificationDate>    

        <sourceHttp>687474703a2f2f7777772e676f6f676c652e69742f</sourceHttp>
            <type>example</type>
            <public>true</public>
    </metadata>
    <annotationBody>
        <contents id="1">
            <textContent>Das ist ein Beispieltext für eine Annotation</textContent>
            <attachments>
               <attachedImage> file:://A/B/C</attachedImage>
            </attachments>
        </contents>

        <contents id="2">
            <md:textContent> Eine weitere Annotation </md:textContent>
            <attachments>
                <attachedAudio>http://www.h_da.de/xml/test.mp3</attachedAudio>
            </attachments>
        </contents>

        <textSelection>
            <path>BODY/CENTER/FORM/TABLE[2]/TBODY/TR[2]/TD/FONT/LABEL[3],23,6</path>
            <contentRef>1</contentRef>
        </textSelection>
    </annotationBody>
</madcow>

Upvotes: 1

Related Questions