XML Namespaces

XML Namespaces

XML Namespaces help avoid name conflicts by providing a way to distinguish elements and attributes with the same name but different meanings.


1. Defining an XML Namespace

A namespace is defined using the xmlns attribute:

<books xmlns="http://example.com/books">
    <book>
        <title>XML Basics</title>
        <author>John Doe</author>
    </book>
</books>
  • The xmlns="http://example.com/books" assigns a default namespace to all child elements.

2. Using Multiple Namespaces

You can define multiple namespaces using prefixes:

<library xmlns:bk="http://example.com/books" xmlns:mag="http://example.com/magazines">
    <bk:book>
        <bk:title>XML Guide</bk:title>
        <bk:author>Jane Doe</bk:author>
    </bk:book>
    <mag:magazine>
        <mag:title>Tech Monthly</mag:title>
    </mag:magazine>
</library>
  • bk: refers to http://example.com/books
  • mag: refers to http://example.com/magazines

3. Accessing Namespaced XML in JavaScript

When working with namespaced XML in JavaScript, you need to use getElementsByTagNameNS():

Example JavaScript to Parse Namespaced XML

let xhr = new XMLHttpRequest();
xhr.open("GET", "data.xml", true);
xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
        let xmlDoc = xhr.responseXML;
        let books = xmlDoc.getElementsByTagNameNS("http://example.com/books", "book");
        let output = "<ul>";
        for (let i = 0; i < books.length; i++) {
            let title = books[i].getElementsByTagNameNS("http://example.com/books", "title")[0].textContent;
            output += "<li>" + title + "</li>";
        }
        output += "</ul>";
        document.getElementById("output").innerHTML = output;
    }
};
xhr.send();

This correctly selects elements within a specific namespace.


4. Namespace in XSD (XML Schema)

If validating with XSD, you must declare the namespace in the schema:

Example XSD (schema.xsd)

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           targetNamespace="http://example.com/books" 
           xmlns="http://example.com/books" 
           elementFormDefault="qualified">
    <xs:element name="books">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="title" type="xs:string"/>
                            <xs:element name="author" type="xs:string"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

This ensures all books elements conform to the namespace.


Key Takeaways

  • Default namespace (xmlns) applies to all child elements.
  • Prefixed namespace (xmlns:prefix) allows multiple namespaces.
  • JavaScript uses getElementsByTagNameNS() to handle namespaces.
  • XML Schema (XSD) must declare targetNamespace for validation.

Would you like an example of namespace handling in XSLT?

Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment