Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 39 ยท XML, XPath and XSLT

XML Namespaces, Entities and Processing Instructions

Understand XML namespaces and prefixes, default namespaces, why namespace URIs matter, custom entities, processing instructions, xml:lang and xml:space, with runnable examples.

Once different vocabularies meet in one document, names can clash: is <title> a book title or a person's honorific? Namespaces solve this by attaching a unique identifier to each vocabulary. This lesson covers namespaces plus the rest of the XML toolbox: entities, processing instructions and the reserved xml: attributes.

Why namespaces

Imagine an XHTML page that embeds an SVG image. Both define a <title> element with different meanings. A namespace gives each vocabulary a unique name, written as a URI, and elements are qualified by it.

XML
<page xmlns="http://www.w3.org/1999/xhtml"
      xmlns:svg="http://www.w3.org/2000/svg">
  <title>Home</title>
  <svg:svg width="40" height="40">
    <svg:title>A red circle</svg:title>
    <svg:circle cx="20" cy="20" r="18" fill="red"/>
  </svg:svg>
</page>

Two declarations appear:

  • xmlns="..." sets the default namespace for the element and all descendants without a prefix.
  • xmlns:svg="..." binds the prefix svg to a namespace.

The namespace URI is only an identifier. It need not point to a real web page, and the parser never fetches it. What matters is that two documents using the same URI mean the same vocabulary, whatever prefix they choose.

Qualified names

A qualified name (QName) is prefix:local. The real identity of an element is the pair (namespace URI, local name), so <svg:circle> and <s:circle> are the same element when both prefixes map to the same URI.

Node.js Resolving prefixes to namespace URIs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the two title elements resolve to different namespaces, so software can tell them apart.

Attributes and namespaces

An unprefixed attribute belongs to no namespace; the default namespace does not apply to attributes. Prefixed attributes such as xlink:href or xml:lang are qualified.

Predefined xml: attributes

The xml prefix is built in and always bound; no declaration is needed.

Attribute Purpose
xml:lang the language of the content, e.g. xml:lang="de"
xml:space preserve asks applications to keep whitespace
xml:base base URI for relative links
xml:id an attribute that is an ID in any vocabulary

Entities

Besides the five predefined entities, a document may declare its own in a DTD:

XML
<!DOCTYPE note [
  <!ENTITY company "Example Software Ltd.">
]>
<note>&company; welcomes you.</note>

The parser replaces &company; with the declared text. Entities are convenient, but external entities (which load a file or URL) are a notorious attack vector; Parsing, security and tooling covers how to switch them off. For now, remember: do not enable DTD processing for untrusted XML.

Node.js Expanding internal entities (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Processing instructions

A processing instruction (PI) passes information to an application: <?target data?>. The declaration <?xml ...?> looks like one but is not. The best-known PI links a stylesheet:

XML
<?xml-stylesheet type="text/xsl" href="catalog.xsl"?>

Comments (<!-- ... -->) are for people and cannot contain --.

Reading real-world namespaced XML

An Atom feed is a typical case: elements live in http://www.w3.org/2005/Atom.

Node.js Reading a namespaced Atom-style feed

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the purpose of an XML namespace?

    A namespace gives each vocabulary a unique identifier so same-named elements stay distinct.
  2. 2. Does the parser fetch the namespace URI?

    The URI is just a unique name; it need not resolve to anything.
  3. 3. Does a default namespace (xmlns="...") apply to unprefixed attributes?

    Unprefixed attributes belong to no namespace.
  4. 4. Why are external entities dangerous?

    XXE attacks abuse external entities to leak files or make network requests.

Summary

  • Namespaces qualify names with a URI; prefixes are only local shortcuts.
  • xmlns sets a default namespace, xmlns:p binds a prefix; attributes without a prefix have none.
  • The xml: prefix gives lang, space, base and id.
  • Entities and DTDs are convenient but risky; processing instructions talk to applications.

Next: DTD and XML Schema