Internet Technologies Xml Namespaces Internet Technologies 1 Notes

  • Slides: 24
Download presentation
Internet Technologies Xml Namespaces Internet Technologies 1

Internet Technologies Xml Namespaces Internet Technologies 1

Notes on XML Namespaces Namespace notes taken and adapted from “XML in a Nutshell”

Notes on XML Namespaces Namespace notes taken and adapted from “XML in a Nutshell” By Harold and Means Java examples adapted from “XML and Java” Namespace specification is at: http: //www. w 3. org/TR/REC-xml-names/ Internet Technologies 2

Namespaces Primary purpose: To disambiguate element and attribute names. Implementation: • • Attach a

Namespaces 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 URI’s may also be provided for those elements with no prefix. The URI’s partition the elements and attributes into 3 disjoint sets. Internet Technologies

Namespaces • • • Each prefix is associated with one URI. Names with prefixes

Namespaces • • • 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. Internet Technologies 4

Namespaces • Prefixes are bound to namespace URI’s by attaching an xmlns: prefix attribute

Namespaces • Prefixes are bound to namespace URI’s by attaching an xmlns: prefix attribute to the prefixed element or one of its ancestors For example: <rdf: RDF xmlns: rdf=http: //www. w 3. org/TR/REC-rdf-syntax#> associates the prefix rdf with the namespace URI shown. The name RDF is therefore an element from this namespace. Internet Technologies 5

Namespaces • Bindings have scope within the element in which they’re declared and its

Namespaces • Bindings have scope within the element in which they’re declared and its contents. <rdf: RDF xmlns: rdf=http: //www. w 3. org/TR/REC-rdf-syntax#> <!– within this element the prefix rdf is associated with the RDF namespace </rdf: RDF> Internet Technologies 6

Namespaces The default namespace • • Is set using the xmlns attribute (with no

Namespaces The default namespace • • Is set using the xmlns attribute (with no prefix) Applies only to elements not attributes <Some. Tag xmlns=“some. URI”> <inside. Tag> … </inside. Tag> </Some. Tag> Some. Tag and inside. Tag are both in the some. URI namespace. Internet Technologies 7

Namespaces If there is no default namespace is declared then tags without Prefixes are

Namespaces 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. <rdf: RDF xmlns: rdf=“http: //www. w 3. org/TR/REC-rdf-syntax#> <rdf: Description about=“some. Value”> : </rdf: Description> The about attribute is </rdf: RDF> in no namespace. Internet Technologies 8

Declaring Namespaces xmlns: pre=“some. URN” is fine xmlns: pre=“” is illegal xmlns=“some. URN” is

Declaring Namespaces xmlns: pre=“some. URN” is fine xmlns: pre=“” is illegal xmlns=“some. URN” is fine xmlns=“” legal and same as no namespace Internet Technologies 9

Some Examples From The W 3 C Specification <x xmlns: edi='http: //ecommerce. org/schema'> <!--

Some Examples From The W 3 C Specification <x xmlns: edi='http: //ecommerce. org/schema'> <!-- the 'price' element's namespace is http: //ecommerce. org/schema --> <edi: price units='Euro'>32. 18</edi: price> </x> Internet Technologies 10

<x xmlns: edi='http: //ecommerce. org/schema'> <!-- the 'tax. Class' attribute's namespace is http: //ecommerce.

<x xmlns: edi='http: //ecommerce. org/schema'> <!-- the 'tax. Class' attribute's namespace is http: //ecommerce. org/schema --> <line. Item edi: tax. Class="exempt">Baby food</line. Item> </x> Internet Technologies 11

<? xml version="1. 0"? > <!-- all elements here are explicitly in the HTML

<? xml version="1. 0"? > <!-- all elements here are explicitly in the HTML namespace --> <html: html xmlns: html='http: //www. w 3. org/TR/REC-html 40'> <html: head><html: title>Frobnostication</html: title> </html: head> <html: body><html: p>Moved to <html: a href='http: //frob. com'>here. </html: a></html: p> </html: body> </html: html> Internet Technologies 12

<? xml version="1. 0"? > <!-- both namespace prefixes are available throughout --> <bk:

<? xml version="1. 0"? > <!-- both namespace prefixes are available throughout --> <bk: book xmlns: bk='urn: loc. gov: books' xmlns: isbn='urn: ISBN: 0 -395 -36341 -6'> <bk: title>Cheaper by the Dozen</bk: title> <isbn: number>1568491379</isbn: number> </bk: book> Internet Technologies 13

<? xml version="1. 0"? > <!-- elements are in the HTML namespace, in this

<? xml version="1. 0"? > <!-- elements are in the HTML namespace, in this case by default --> <html xmlns='http: //www. w 3. org/TR/REC-html 40'> <head><title>Frobnostication</title></head> <body><p>Moved to <a href='http: //frob. com'>here</a>. </p></body> </html> Internet Technologies 14

<? xml version="1. 0"? > <!-- unprefixed element types are from "books" --> <book

<? xml version="1. 0"? > <!-- unprefixed element types are from "books" --> <book xmlns='urn: loc. gov: books' xmlns: isbn='urn: ISBN: 0 -395 -36341 -6'> <title>Cheaper by the Dozen</title> <isbn: number>1568491379</isbn: number> </book> Internet Technologies 15

<? xml version="1. 0"? > <!-- initially, the default namespace is "books" --> <book

<? xml version="1. 0"? > <!-- initially, the default namespace is "books" --> <book xmlns='urn: loc. gov: books' xmlns: isbn='urn: ISBN: 0 -395 -36341 -6'> <title>Cheaper by the Dozen</title> <isbn: number>1568491379</isbn: number> <notes> <!-- make HTML the default namespace for some commentary --> <p xmlns='urn: w 3 -org-ns: HTML'> This is a <i>funny</i> book! </p> </notes> </book> Internet Technologies 16

<? xml version='1. 0'? > <Beers> <!-- the default namespace is now that of

<? xml version='1. 0'? > <Beers> <!-- the default namespace is now that of HTML --> <table xmlns='http: //www. w 3. org/TR/REC-html 40'> <th><td>Name</td><td>Origin</td><td>Description</td></th> <tr> <!-- no default namespace inside table cells --> <td><brand. Name xmlns="">Huntsman</brand. Name></td> <td><origin xmlns="">Bath, UK</origin></td> <td> <details xmlns=""><class>Bitter</class><hop>Fuggles</hop> <pro>Wonderful hop, light alcohol, good summer beer</pro> <con>Fragile; excessive variance pub to pub</con> </details> </td> The default namespace can be set to the empty </tr> string. This has the same effect, within the scope </table> Internet Technologies of the declaration, of there being no default 17 </Beers>

<!-- http: //www. w 3. org is bound to n 1 and n 2

<!-- http: //www. w 3. org is bound to n 1 and n 2 --> <x xmlns: n 1="http: //www. w 3. org" xmlns: n 2="http: //www. w 3. org" > <bad a="1" a="2" /> <bad n 1: a="1" n 2: a="2" /> </x> Internet Technologies 18

<!-- http: //www. w 3. org is bound to n 1 and is the

<!-- http: //www. w 3. org is bound to n 1 and is the default --> <x xmlns: n 1="http: //www. w 3. org" xmlns="http: //www. w 3. org" > <good a="1" b="2" /> <good a="1" n 1: a="2" /> </x> Internet Technologies 19

Namespaces and Java // Exploring the Namespace. Corrector class in Chapter 4 of //

Namespaces and Java // Exploring the Namespace. Corrector class in Chapter 4 of // XML and Java // Example 1 import javax. xml. parsers. Document. Builder; import javax. xml. parsers. Document. Builder. Factory; import org. apache. xml. serialize. Output. Format; import org. apache. xml. serialize. XMLSerializer; import org. w 3 c. dom. Document; import org. w 3 c. dom. Element; import org. w 3 c. dom. Text; Internet Technologies 20

public class Namespace. Explore { static final String NS = "http: //www. andrew. cmu.

public class Namespace. Explore { static final String NS = "http: //www. andrew. cmu. edu/~mm 6"; // the assigned namespace to the xml: lang attribute static final String XML_NS = "http: //www. w 3. org/XML/1998/namespace"; // the assigned namespace of the xmlns attribute static final String XMLNS_NS = "http: //www. w 3. org/2000/xmlns/"; Internet Technologies 21

public static void main(String[] argv) throws Exception { Document. Builder. Factory dbfactory = Document.

public static void main(String[] argv) throws Exception { Document. Builder. Factory dbfactory = Document. Builder. Factory. new. Instance(); dbfactory. set. Namespace. Aware(true); Document. Builder builder = dbfactory. new. Document. Builder(); Document factory = builder. new. Document(); Output. Format format = new Output. Format("xml", "UTF-8", true); XMLSerializer serializer = new XMLSerializer(System. out, format); // build a top element within a namespace Element top = factory. create. Element. NS(NS, "mm 6: Grade. Book"); Internet Technologies 22

// define an xmlns attribute within this top element top. set. Attribute. NS(XMLNS_NS, "xmlns:

// define an xmlns attribute within this top element top. set. Attribute. NS(XMLNS_NS, "xmlns: mm 6", NS); // define an xml: lang attribute within this top element top. set. Attribute. NS(XML_NS, "xml: lang", "en"); Element student = factory. create. Element. NS(NS, "mm 6: Student"); top. append. Child(student); Text t = factory. create. Text. Node("87. 5"); student. append. Child(t); serializer. serialize(top); System. out. println(""); } } Internet Technologies 23

Output D: Mc. Carthywww95 -733exampleschap 04>java Namespace. Explore <? xml version="1. 0" encoding="UTF-8"? >

Output D: Mc. Carthywww95 -733exampleschap 04>java Namespace. Explore <? xml version="1. 0" encoding="UTF-8"? > <mm 6: Grade. Book xml: lang="en" xmlns: mm 6= "http: //www. andrew. cmu. edu/~mm 6"> <mm 6: Student>87. 5</mm 6: Student> </mm 6: Grade. Book> Internet Technologies 24