Namespaces

Definition

Goal

  1. making elements and attributes unique

    where they have the same name but have different semantics

  1. grouping elements

    so that programs can easily recognize them

Syntax

define the name behind a prefix in parent’s tag: <... xmlns:prefix="name">

can be set as default prefix through <... xmlns="name">

then use that prefix in children: <prefix:childName>

  • name = URI
  • prefix = XML name

Multiple namespaces

We can redefine the namespace inside the children themselves

Example

We want to merge assessments of the same course.

Problem: we can’t tell who they are from without comments.

<course>
<title> SSD </title>
<assessment> Fair </assessment> <!--Students' Evaluation-->
<assessment> Top Priority </assessment> <!--University's Evaluation-->
</course>

Renaming

<studasessment>,<univassessment or <stud:asessment>,<univ:assessment>

may still cause conflicts in the future!

Namespaces

prefixes stud and univ as unique URLs

<course
xmlns:stud="http://www.oeh.ac.at"
xmlns:univ= "http://www.tuwien.ac.at">
<title> SSD </title>
<stud:assessment> Fair </stud:assessment >
<univ:assessment> Top Priority </univ:assessment >
</course>

Then the expanded namespaces would be:

{http://www.oeh.ac.at}assessment for stud:assessment

{http://www.tuwien.ac.at}assessment for univ:assessment

Default namespaces

by removing the prefix in the definition

useful to avoid conflicts when merging multiple XML documents

<course
xmlns="http://www.oeh.ac.at"
xmlns:univ= "http://www.tuwien.ac.at">
<title> SSD </title>
<assessment> Fair </assessment >
<univ:assessment> Top Priority </univ:assessment >
</course>

Multiple namespaces

ie. by overwriting the default namespaces in the children themselves.

<course	xmlns="http://www.oeh.ac.at">
<title> SSD </title>
<assessment> Fair </assessment >
<assessment xmlns="http://www.tuwien.ac.at"> Top Priority </assessment >
</course>