internet technologies
DESCRIPTION
Internet Technologies. Xml Namespaces. 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.w3.org/TR/REC-xml-names/. Namespaces. Primary purpose: - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/1.jpg)
Internet Technologies 1
Internet Technologies
Xml Namespaces
![Page 2: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/2.jpg)
Internet Technologies 2
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.w3.org/TR/REC-xml-names/
![Page 3: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/3.jpg)
Internet Technologies 3
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
disjoint sets.
![Page 4: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/4.jpg)
Internet Technologies 4
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.
![Page 5: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/5.jpg)
Internet Technologies 5
Namespaces
• Prefixes are bound to namespace URI’s by attaching an xmlns:prefix attribute to the the prefixed elementor one of its ancestors
For example:
<rdf:RDF xmlns:rdf=http://www.w3.org/TR/REC-rdf-syntax#>
associates the prefix rdf with the namespace URI shown. Thename RDF is therefore an element from this namespace.
![Page 6: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/6.jpg)
Internet Technologies 6
Namespaces
• Bindings have scope within the element in which they’redeclared and its contents.
<rdf:RDF xmlns:rdf=http://www.w3.org/TR/REC-rdf-syntax#>
<!– within this element the prefix rdf is associated with the RDF namespace
</rdf:RDF>
![Page 7: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/7.jpg)
Internet Technologies 7
Namespaces
The default namespace
• Is set using the xmlns attribute (with no prefix)• Applies only to elements not attributes
<SomeTag xmlns=“someURI”> <insideTag> … </insideTag> </SomeTag>
SomeTag and insideTag are both in the someURI namespace.
![Page 8: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/8.jpg)
Internet Technologies 8
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.w3.org/TR/REC-rdf-syntax#> <rdf:Description about=“someValue”> : </rdf:Description>
</rdf:RDF>
The about attribute isin no namespace.
![Page 9: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/9.jpg)
Internet Technologies 9
Declaring Namespaces
xmlns:pre=“someURN” is finexmlns:pre=“” is illegal
xmlns=“someURN” is finexmlns=“” legal and same as no namespace
![Page 10: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/10.jpg)
Internet Technologies 10
Some Examples From The W3C 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>
![Page 11: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/11.jpg)
Internet Technologies 11
<x xmlns:edi='http://ecommerce.org/schema'> <!-- the 'taxClass' attribute's namespace is http://ecommerce.org/schema --> <lineItem edi:taxClass="exempt">Baby food</lineItem></x>
![Page 12: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/12.jpg)
Internet Technologies 12
<?xml version="1.0"?><!-- all elements here are explicitly in the HTML namespace --><html:html xmlns:html='http://www.w3.org/TR/REC-html40'> <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>
![Page 13: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/13.jpg)
Internet Technologies 13
<?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>
![Page 14: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/14.jpg)
Internet Technologies 14
<?xml version="1.0"?><!-- elements are in the HTML namespace, in this case by default --><html xmlns='http://www.w3.org/TR/REC-html40'> <head><title>Frobnostication</title></head> <body><p>Moved to <a href='http://frob.com'>here</a>.</p></body></html>
![Page 15: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/15.jpg)
Internet Technologies 15
<?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>
![Page 16: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/16.jpg)
Internet Technologies 16
<?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:w3-org-ns:HTML'> This is a <i>funny</i> book! </p> </notes></book>
![Page 17: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/17.jpg)
Internet Technologies 17
<?xml version='1.0'?><Beers> <!-- the default namespace is now that of HTML --> <table xmlns='http://www.w3.org/TR/REC-html40'> <th><td>Name</td><td>Origin</td><td>Description</td></th> <tr> <!-- no default namespace inside table cells --> <td><brandName xmlns="">Huntsman</brandName></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> </tr> </table> </Beers>
The default namespace can be set to the empty string. This has the same effect, within the scopeof the declaration, of there being no default namespace.
![Page 18: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/18.jpg)
Internet Technologies 18
<!-- http://www.w3.org is bound to n1 and n2 --><x xmlns:n1="http://www.w3.org" xmlns:n2="http://www.w3.org" > <bad a="1" a="2" /> <bad n1:a="1" n2:a="2" /></x>
![Page 19: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/19.jpg)
Internet Technologies 19
<!-- http://www.w3.org is bound to n1 and is the default --><x xmlns:n1="http://www.w3.org" xmlns="http://www.w3.org" > <good a="1" b="2" /> <good a="1" n1:a="2" /></x>
![Page 20: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/20.jpg)
Internet Technologies 20
Namespaces and Java
// Exploring the NamespaceCorrector class in Chapter 4 of// XML and Java
// Example 1
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;import org.w3c.dom.Text;
![Page 21: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/21.jpg)
Internet Technologies 21
public class NamespaceExplore {
static final String NS = "http://www.andrew.cmu.edu/~mm6"; // the assigned namespace to the xml:lang attribute static final String XML_NS = "http://www.w3.org/XML/1998/namespace";
// the assigned namespace of the xmlns attribute static final String XMLNS_NS = "http://www.w3.org/2000/xmlns/";
![Page 22: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/22.jpg)
Internet Technologies 22
public static void main(String[] argv) throws Exception {
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance(); dbfactory.setNamespaceAware(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, "mm6:GradeBook");
![Page 23: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/23.jpg)
Internet Technologies 23
// define an xmlns attribute within this top element top.setAttributeNS(XMLNS_NS, "xmlns:mm6", NS);
// define an xml:lang attribute within this top element top.setAttributeNS(XML_NS, "xml:lang", "en");
Element student = factory.createElementNS(NS,"mm6:Student"); top.appendChild(student);
Text t = factory.createTextNode("87.5"); student.appendChild(t);
serializer.serialize(top); System.out.println(""); }}
![Page 24: Internet Technologies](https://reader035.vdocuments.us/reader035/viewer/2022062518/56814a15550346895db73b55/html5/thumbnails/24.jpg)
Internet Technologies 24
D:\McCarthy\www\95-733\examples\chap04>java NamespaceExplore
<?xml version="1.0" encoding="UTF-8"?><mm6:GradeBook xml:lang="en" xmlns:mm6= "http://www.andrew.cmu.edu/~mm6"> <mm6:Student>87.5</mm6:Student></mm6:GradeBook>
Output