Title: OCT
1OCT
Xml 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