Presentation is loading. Please wait.

Presentation is loading. Please wait.

Internet Technologies1 Xml Namespaces. Internet Technologies2 Notes on XML Namespaces Namespace notes taken and adapted from “XML in a Nutshell” By Harold.

Similar presentations


Presentation on theme: "Internet Technologies1 Xml Namespaces. Internet Technologies2 Notes on XML Namespaces Namespace notes taken and adapted from “XML in a Nutshell” By Harold."— Presentation transcript:

1 Internet Technologies1 Xml Namespaces

2 Internet Technologies2 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/

3 Internet Technologies3 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.

4 Internet Technologies4 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.

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

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

7 Internet Technologies7 Namespaces The default namespace Is set using the xmlns attribute (with no prefix) Applies only to elements not attributes … SomeTag and insideTag are both in the someURI namespace.

8 Internet Technologies8 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. http://www.w3.org/TR/REC-rdf-syntax# : The about attribute is in no namespace.

9 Internet Technologies9 Declaring Namespaces xmlns:pre=“someURN” is fine xmlns:pre=“” is illegal xmlns=“someURN” is fine xmlns=“” legal and same as no namespace

10 Internet Technologies10 Some Examples From The W3C Specification <!-- the 'price' element's namespace is http://ecommerce.org/schema --> 32.18

11 Internet Technologies11 <!-- the 'taxClass' attribute's namespace is http://ecommerce.org/schema --> Baby food

12 Internet Technologies12 Frobnostication Moved to here.

13 Internet Technologies13 Cheaper by the Dozen 1568491379

14 Internet Technologies14 <!-- elements are in the HTML namespace, in this case by default --> Frobnostication Moved to here.

15 Internet Technologies15 Cheaper by the Dozen 1568491379

16 Internet Technologies16 Cheaper by the Dozen 1568491379 <!-- make HTML the default namespace for some commentary --> This is a funny book!

17 Internet Technologies17 Name Origin Description Huntsman Bath, UK Bitter Fuggles Wonderful hop, light alcohol, good summer beer Fragile; excessive variance pub to pub 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.

18 Internet Technologies18

19 Internet Technologies19

20 Internet Technologies20 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;

21 Internet Technologies21 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/";

22 Internet Technologies22 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");

23 Internet Technologies23 // 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(""); }

24 Internet Technologies24 D:\McCarthy\www\95-733\examples\chap04>java NamespaceExplore <mm6:GradeBook xml:lang="en" xmlns:mm6= "http://www.andrew.cmu.edu/~mm6"> 87.5 Output


Download ppt "Internet Technologies1 Xml Namespaces. Internet Technologies2 Notes on XML Namespaces Namespace notes taken and adapted from “XML in a Nutshell” By Harold."

Similar presentations


Ads by Google