Title: Web Technologies
1 Web Technologies
Week Four Advanced DOM and Working with
namespaces
2Notes on XML Namespaces
Namespace notes taken and adapted from XML in a
Nutshell By Harold and Means Java examples
adapted from XML and Java course text
Namespace specification is at http//www.w3.org/
TR/REC-xml-names/
3Namespaces
- Primary purpose
- To disambiguate element and attribute names.
- Implementation
- Attach a prefix to an element or attribute name.
- Map the prefix to a URI. This need not be a real
place. - Default URIs may also be provided for those
- elements with no prefix.
- The URIs partition the elements and attributes
into - disjoint sets.
4Namespaces
- Each prefix is associated with one URI.
- Names with prefixes associated with the
same URI are - in the same namespace.
- Elements and attributes in namespaces have names
with - exactly one colon.
- The text before the colon is called the
prefix. - The text after the colon is called the
local part. - The complete name, including the colon,
is called the - qualified name.
5Namespaces
- Prefixes are bound to namespace URIs by
attaching - an xmlnsprefix attribute to the the prefixed
element - or one of its ancestors
- For example
- ltrdfRDF xmlnsrdfhttp//www.w3.org/TR
/REC-rdf-syntaxgt - associates the prefix rdf with the namespace URI
shown. The - name RDF is therefore an element from this
namespace.
6Namespaces
- Bindings have scope within the element in which
theyre - declared and its contents.
- ltrdfRDF xmlnsrdfhttp//www.w3.org/TR/REC-rdf-sy
ntaxgt - lt! within this element the prefix rdf is
associated - with the RDF namespace ?
-
- lt/rdfRDFgt
7Namespaces
- The default namespace
- Is set using the xmlns attribute (with no
prefix) - Applies only to elements not attributes
- ltSomeTag xmlnssomeURIgt
- ltinsideTaggt lt/insideTaggt
- lt/SomeTaggt
- SomeTag and insideTag are both in the
someURI namespace.
8Namespaces
If there is no default namespace is declared then
tags without Prefixes are in no namespace at
all. Not even the default one. The only way an
attribute belongs to a namespace is if it has a
declared prefix. ltrdfRDF xmlnsrdfhttp//w
ww.w3.org/TR/REC-rdf-syntaxgt
ltrdfDescription aboutsomeValuegt
lt/rdfDescriptiongt lt/rdfRDFgt
The about attribute is in no namespace.
9Declaring Namespaces
xmlnspresomeURN is fine xmlnspre is
illegal xmlnssomeURN is fine xmlns legal
and same as no namespace
10Some Examples From The W3C Specification
ltx xmlnsedi'http//ecommerce.org/schema'gt lt!--
the 'price' element's namespace is
http//ecommerce.org/schema --gt ltediprice units
'Euro'gt32.18lt/edipricegtlt/xgt
11ltx xmlnsedi'http//ecommerce.org/schema'gt lt!--
the 'taxClass' attribute's namespace is
http//ecommerce.org/schema --gt ltlineItem edit
axClass"exempt"gtBaby foodlt/lineItemgtlt/xgt
12lt?xml version"1.0"?gtlt!-- all elements here are e
xplicitly in the HTML namespace --gtlthtmlhtml xml
nshtml'http//www.w3.org/TR/REC-html40'gt lthtml
headgtlthtmltitlegtFrobnosticationlt/htmltitlegt
lt/htmlheadgt lthtmlbodygtlthtmlpgtMoved to lth
tmla href'http//frob.com'gthere.lt/htmlagtlt/html
pgt lt/htmlbodygtlt/htmlhtmlgt
13lt?xml version"1.0"?gtlt!-- both namespace prefixes
are available throughout --gtltbkbook xmlnsbk'u
rnloc.govbooks'
xmlnsisbn'urnISBN0-395-36341-6'gt ltbktitle
gtCheaper by the Dozenlt/bktitlegt ltisbnnumbergt
1568491379lt/isbnnumbergtlt/bkbookgt
14lt?xml version"1.0"?gtlt!-- elements are in the HTM
L namespace, in this case
by default --gtlthtml xmlns'http//www.w3.org/TR/R
EC-html40'gt ltheadgtlttitlegtFrobnosticationlt/titlegt
lt/headgt ltbodygtltpgtMoved to lta href'http//f
rob.com'gtherelt/agt.lt/pgtlt/bodygtlt/htmlgt
15lt?xml version"1.0"?gtlt!-- unprefixed element type
s are from "books" --gtltbook xmlns'urnloc.govbo
oks' xmlnsisbn'urnISBN0-395-36341-6'gt
lttitlegtCheaper by the Dozenlt/titlegt ltisbnnu
mbergt1568491379lt/isbnnumbergtlt/bookgt
16lt?xml version"1.0"?gtlt!-- initially, the default
namespace is "books" --gtltbook xmlns'urnloc.gov
books' xmlnsisbn'urnISBN0-395-36341-6'gt
lttitlegtCheaper by the Dozenlt/titlegt ltisbn
numbergt1568491379lt/isbnnumbergt ltnotesgt
lt!-- make HTML the default namespace for
some commentary --gt ltp xmlns'urnw3-or
g-nsHTML'gt This is a ltigtfunnylt/igt book!
lt/pgt lt/notesgtlt/bookgt
17lt?xml version'1.0'?gtltBeersgt lt!-- the default n
amespace is now that of HTML --gt lttable xmlns'h
ttp//www.w3.org/TR/REC-html40'gt ltthgtlttdgtNamelt/
tdgtlttdgtOriginlt/tdgtlttdgtDescriptionlt/tdgtlt/thgt lttr
gt lt!-- no default namespace inside table cel
ls --gt lttdgtltbrandName xmlns""gtHuntsmanlt/bran
dNamegtlt/tdgt lttdgtltorigin xmlns""gtBath, UKlt/or
igingtlt/tdgt lttdgt ltdetails xmlns""gtltcla
ssgtBitterlt/classgtlthopgtFuggleslt/hopgt ltprogt
Wonderful hop, light alcohol, good summer beerlt/pr
ogt ltcongtFragile excessive variance pub t
o publt/congt lt/detailsgt lt/tdgt
lt/trgt lt/tablegt lt/Beersgt
The default namespace can be set to the empty
string. This has the same effect, within the
scope of the declaration, of there being no
default namespace.
18lt!-- http//www.w3.org is bound to n1 and n2 --gtlt
x xmlnsn1"http//www.w3.org"
xmlnsn2"http//www.w3.org" gt ltbad a"1" a
"2" /gt ltbad n1a"1" n2a"2" /gtlt/xgt
19lt!-- http//www.w3.org is bound to n1 and is the d
efault --gtltx xmlnsn1"http//www.w3.org"
xmlns"http//www.w3.org" gt
ltgood a"1" b"2" /gt ltgood a"1" n1a
"2" /gtlt/xgt
20Namespaces and Java
// Exploring the NamespaceCorrector class in
Chapter 4 of // XML and Java // Example
1 import javax.xml.parsers.DocumentBuilder impor
t javax.xml.parsers.DocumentBuilderFactory import
org.apache.xml.serialize.OutputFormat import
org.apache.xml.serialize.XMLSerializer import
org.w3c.dom.Document import org.w3c.dom.Element
import org.w3c.dom.Text
21 public class NamespaceExplore static
final String NS "http//www.andrew.cmu.edu/mm6"
// the assigned namespace to the
xmllang attribute static final String XML_NS
"http//www.w3.org/XML/1998/namespac
e" // the assigned namespace of the xmlns
attribute static final String XMLNS_NS
"http//www.w3.org/2000/xmlns/"
22public static void main(String argv) throws
Exception DocumentBuilderFactory
dbfactory DocumentBuilderFactory
.newInstance() dbfactory.setNamespaceAwar
e(true) DocumentBuilder builder
dbfactory.newDocumentBuilder() Document
factory builder.newDocument()
OutputFormat format new
OutputFormat("xml", "UTF-8", true)
XMLSerializer serializer new
XMLSerializer(System.out, format) //
build a top element within a namespace
Element top factory.createElementNS(NS,
"mm6GradeBook")
23 // define an xmlns attribute within this
top element top.setAttributeNS(XMLNS_NS,
"xmlnsmm6", NS) // define an xmllang
attribute within this top element
top.setAttributeNS(XML_NS, "xmllang", "en")
Element student factory.createElementNS(NS,
"mm6Student") top.appendChild(student)
Text t factory.createTextNode("87.5")
student.appendChild(t)
serializer.serialize(top)
System.out.println("")
24Output
D\McCarthy\www\95-733\examples\chap04gtjava
NamespaceExplore lt?xml version"1.0"
encoding"UTF-8"?gt ltmm6GradeBook xmllang"en"
xmlnsmm6
"http//www.andrew.cmu.edu/mm6"gt
ltmm6Studentgt87.5lt/mm6Studentgt lt/mm6GradeBookgt
25Attributes and Namespaces
// Exploring the NamespaceCorrector class in
Chapter 4 of // XML and Java // Example 2
Looking at Attributes import javax.xml.parsers.Do
cumentBuilder import javax.xml.parsers.DocumentBu
ilderFactory import org.apache.xml.serialize.Outp
utFormat import org.apache.xml.serialize.XMLSeria
lizer import org.w3c.dom.Document import
org.w3c.dom.Element import org.w3c.dom.Text impo
rt org.w3c.dom.NamedNodeMap import
org.w3c.dom.Attr
26public class NamespaceExplore2 static
final String NS "http//www.andrew.cmu.edu/mm6"
// the assigned namespace to the
xmllang attribute static final String XML_NS
"http//www.w3.org/XML/1998/namespac
e" // the assigned namespace of the xmlns
attribute static final String XMLNS_NS
"http//www.w3.org/2000/xmlns/"
Same as before
27public static void main(String argv) throws
Exception DocumentBuilderFactory
dbfactory DocumentBuilderFactory
.newInstance() dbfactory.setNamespaceAwar
e(true) DocumentBuilder builder
dbfactory.newDocumentBuilder() Document
factory builder.newDocument()
OutputFormat format new
OutputFormat("xml", "UTF-8", true)
XMLSerializer serializer new
XMLSerializer(System.out, format)
Same as before
28 // build a top element within a
namespace Element top
factory.createElementNS(NS, "mm6GradeBook")
// define an xmlns attribute within this top
element top.setAttributeNS(XMLNS_NS,
"xmlnsmm6", NS) // define an xmllang
attribute withing this top element
top.setAttributeNS(XML_NS, "xmllang", "en")
// create a Student tag Element
student factory.createElementNS(NS,"mm6Student"
) // add a prefixed attribute
to Student student.setAttributeNS(NS,"mm6
StudentID", "123-34-8765")
top.appendChild(student)
We are creating attributes with namespaces.
29 Text t factory.createTextNode("87.5")
student.appendChild(t)
serializer.serialize(top)
System.out.println("") // display the
attributes in the student tag
showNamespaceOfAttributes(student) //
display the attributes in the top tag
showNamespaceOfAttributes(top)
30public static void showNamespaceOfAttributes(Eleme
nt e) System.out.println("Display
attributes of " e.getTagName())
NamedNodeMap map e.getAttributes() for
(int i 0 i lt map.getLength() i)
Attr attr (Attr)map.item(i)
String prefix attr.getPrefix()
String name attr.getName() String
val attr.getValue() String uri
attr.getNamespaceURI()
System.out.println("Attribute gt" name)
System.out.println(" prefix "
prefix) System.out.println("
value " val)
System.out.println(" NS URI " uri)
31Output
D\McCarthy\www\95-733\examples\chap04gtjava
NamespaceExplore2 lt?xml version"1.0"
encoding"UTF-8"?gt ltmm6GradeBook xmllang"en
xmlnsmm6"http//www
.andrew.cmu.edu/mm6"gt ltmm6Student
mm6StudentID"123-34-8765"gt87.5
lt/mm6Studentgt lt/mm6GradeBookgt Display
attributes of mm6Student Attribute
gtmm6StudentID prefix mm6
value 123-34-8765 NS URI
http//www.andrew.cmu.edu/mm6
32Display attributes of mm6GradeBook Attribute
gtxmllang prefix xml
value en NS URI http//www.w3.org/X
ML/1998/namespace Attribute gtxmlnsmm6
prefix xmlns value
http//www.andrew.cmu.edu/mm6 NS URI
http//www.w3.org/2000/xmlns/
33NCTest.java XML and Java
// NCTest.java Listing 4.2 XML and Java import
javax.xml.parsers.DocumentBuilder import
javax.xml.parsers.DocumentBuilderFactory import
org.apache.xml.serialize.OutputFormat import
org.apache.xml.serialize.XMLSerializer import
org.w3c.dom.Document import org.w3c.dom.Element
34public class NCTest static final String NS0
"http//example.com/_at_" static final String
NS1 "http//example.com/a" static final
String XML_NS "http//www.w3.org/XML
/1998/namespace" static final String
XMLNS_NS "http//www.w3.org/2000/xmlns/"
public static void main(String argv) throws
Exception DocumentBuilderFactory
dbfactory DocumentBuilderFactory
.newInstance() dbfactory.setNamespaceAwar
e(true) DocumentBuilder builder
dbfactory.newDocumentBuilder() Document
factory builder.newDocument()
OutputFormat format new
OutputFormat("xml", "UTF-8", true)
XMLSerializer serializer new
XMLSerializer(System.out, format)
35 Element top factory.createElementNS(null
, "Address") top.setAttributeNS(XMLNS_NS,
"xmlnsp", NS0) // Add an element that
has the namespace and no prefix. Element
el1 factory.createElementNS(NS1, "Zip")
// el1 has an attribute of which namespace is
// the same as el1.
el1.setAttributeNS(NS1, "pid", "")
el1.appendChild(factory.createElementNS(null,
"Zip2")) top.appendChild(el1)
// Add an element that has the namespace and
prefix. Element el2 factory.createElemen
tNS(NS1, "pState") // add an attribute
to el2 -- code deleted -- no harm mistake in
book el2.setAttributeNS(XML_NS,
"xmllang", "en") top.appendChild(el2)
36 Element el3 factory.createElementNS(NS0,
"pCity") top.appendChild(el3)
// Prints the tree before correction.
serializer.serialize(top)
System.out.println("") // Correct
NamespaceCorrector.correct(top) //
Prints the tree after correction.
serializer.reset() serializer.serialize(t
op) System.out.println("")
What does the output document look like?
Now, what does the output document look like?
37 // Another test // pCountry and
piso2 have the same prefix but //
different namespaces. Element el4
factory.createElementNS(NS0, "pCountry")
el4.setAttributeNS(NS1, "piso2", "ja")
// This should throw an exception.
NamespaceCorrector.correct(el4)
What does this document look like and why cant
it be repaired?
38D\McCarthy\www\95-733\examples\chap04gtjavac
NCTest.java D\McCarthy\www\95-733\examples\chap0
4gtjava NCTest lt?xml version"1.0"
encoding"UTF-8"?gt ltAddress xmlnsp"http//exampl
e.com/_at_"gt ltZip pid""gt ltZip2/gt
lt/Zipgt ltpState xmllang"en"/gt
ltpCity/gt lt/Addressgt
This is not as the author intended.
39lt?xml version"1.0" encoding"UTF-8"?gt ltAddress
xmlnsp"http//example.com/_at_"gt ltZip pid""
xmlns"http//example.com/a"
xmlnsp"http//example.com/a"gt ltZip2
xmlns""/gt lt/Zipgt ltpState xmllang"en"
xmlnsp"http//example.com/a"/gt
ltpCity/gt lt/Addressgt
Corrected document.
40Exception in thread "main" org.w3c.dom.DOMExceptio
n Namespace inconsistence at
NamespaceCorrector.set(NamespaceCorrector.java89)
at NamespaceCorrector.correctElement(Names
paceCorrector.java78) at
NamespaceCorrector.correct(NamespaceCorrector.java
26) at NCTest.main(NCTest.java67)
Too bad to fix.
41NamespaceCorrector.java
// NamespaceCorrector.java import
org.w3c.dom.Attr import org.w3c.dom.DOMException
import org.w3c.dom.Document import
org.w3c.dom.Element import org.w3c.dom.Node impo
rt org.w3c.dom.NamedNodeMap
42/ Add required namespace declarations.
/ public class NamespaceCorrector private
static final String XMLNS_NS
"http//www.w3.org/2000/xmlns/" private
NamespaceCorrector()
43/ _at_param node The top node of target nodes
/ public static void correct(Node node)
switch (node.getNodeType()) case
Node.ELEMENT_NODE correctElement((Ele
ment)node) //
Fall down case Node.DOCUMENT_NODE
case Node.DOCUMENT_FRAGMENT_NODE case
Node.ENTITY_REFERENCE_NODE for (Node
ch node.getFirstChild() ch !
null ch ch.getNextSibling())
correct(ch)
break
44/ Check whether the prefixes and the
namespaces of el and its attributes are
declared or not. if not, add a namespace
declaration to el. / private static
void correctElement(Element el) //
Check el. String prefix
el.getPrefix() String current
el.getNamespaceURI() String declared
howDeclared(el, prefix)
45if (prefix null) if (current
null declared null) //
ok else if (current null
declared null) set(el,
prefix, current null ? "" current)
else if (!current.equals(declared))
set(el, prefix, current)
else if (current null)
throw new DOMException(
DOMException.NAMESPACE_ERR,
el.getNodeName()
" has no namespace") if
(declared null !current.equals(declared))
set(el, prefix, current)
46 // Check attributes of el.
NamedNodeMap map el.getAttributes()
for (int i 0 i lt map.getLength() i)
Attr attr (Attr)map.item(i)
prefix attr.getPrefix() if
(prefix null prefix.equals("xml")
prefix.equals("xmlns"))
continue current
attr.getNamespaceURI() declared
howDeclared(el, prefix) if (declared
null !current.equals(declared))
set(el, prefix, current) i
-1 // map has changed.
// So restart the loop.
47private static void set(Element el, String
prefix, String ns) String qname
prefix null ? "xmlns" "xmlns"prefix
if (el.getAttributeNode(qname) ! null)
throw new DOMException(DOMException.NAMESPACE
_ERR,
"Namespace inconsistence")
el.setAttributeNS(XMLNS_NS, qname, ns)
48/ Search ltvargtcontextlt/vargt and
ancestors for declaration of prefix.
_at_param prefix Prefix, or ltcodegtnulllt/codegt for
default ns. / private static String
howDeclared(Element context,
String prefix) String
qname prefix null ? "xmlns"
"xmlns"prefix
49for (Node node context node ! null
node node.getParentNode()) if
(node.getNodeType() Node.ELEMENT_NODE)
Attr attr ((Element)node).getAttribute
Node(qname) if (attr ! null)
if (prefix null
attr.getNodeValue().equals(""))
return null
else return
attr.getNodeValue()
return null
50Homework
Trace the NamespaceCorrector.java program for the
following cases Case 1 An element has a
namespace but no prefix. An ancestor of the
element has a default namespace with the same
namespace value. Case 2 An element has a
namespace but no prefix and no default namespace
exists.
51Case 3. An element has a namespace but no prefix
and an ancestor has a different default
namespace. Case 4. An element has a namespace
but no prefix and this element declares a
different default namespace. Case 5. An element
has a namespace and a prefix that points to a
different namespace.