1 / 22

Chapter 3: XML Namespaces

Chapter 3: XML Namespaces. Chapter 3 Objectives. Why you need namespaces What namespaces are, conceptually and how they solve the problem of naming clashes The syntax for using namespaces in XML documents What is a URI, URL, and URN. Why Do We Need Namespaces?. <?xml version=“1.0”?>

zofia
Download Presentation

Chapter 3: XML Namespaces

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: XML Namespaces

  2. Chapter 3 Objectives • Why you need namespaces • What namespaces are, conceptually and how they solve the problem of naming clashes • The syntax for using namespaces in XML documents • What is a URI, URL, and URN

  3. Why Do We Need Namespaces? <?xml version=“1.0”?> <person> <name> <title>Sir</title> <first>John</first> <middle>Fitzgerald Johansen</middle> <last>Doe</last> </name> <position>Vice President of Marketing</position> <résumé> <html> <head><title>Resume of John Doe</title></head> <body> <h1>John Doe</h1> <p>John’s a great guy, you know?</p> </body> </html> </résumé> </person>

  4. Using Prefixes <?xml version=“1.0”?> <pers:person> <pers:name> <pers:title>Sir</pers:title> <pers:first>John</pers:first> <pers:middle>Fitzgerald Johansen</pers:middle> <pers:last>Doe</pers:last> </pers:name> <pers:position>Vice President of Marketing</pers:position> <pers:résumé> <xhtml:html> <xhtml:head><xhtml:title>Resume of John Doe</xhtml:title></xhtml:head> <xhtml:body> <xhtml:h1>John Doe</xhtml:h1> <xhtml:p>John’s a great guy, you know?</xhtml:p> </xhtml:body> </xhtml:html> </pers:résumé> </pers:person>

  5. So Why Doesn’t XML Just Use These Prefixes? <?xml version=“1.0”?> <{http://www.wileycom/pers}person> <{http://www.wileycom/pers}name> <{http://www.wileycom/pers}title> Sir </{http://www.wileycom/pers}title> <!--etc...--> URI's and URN's are used for uniqueness

  6. How XML Namespaces Work <pers:person xmlns:pers=“http://www.wileycom/pers”> <pers:name> <pers:title>Sir</pers:title> </pers:name> </pers:person> Together they are guaranteed to be unique

  7. Default Namespaces <person xmlns=“http://www.wiley.com/pers"> <name> <title>Sir</title> </name> </person> <person xmlns=“http://www.wiley.com/pers” xmlns:xhtml=“http://www.w3.org/1999/xhtml”> <name/> <xhtml:p>This is XHTML</xhtml:p> </person> Default Default except for the <p> tag

  8. Declaring Namespaces on Descendants Example 1 <person xmlns=“http://www.wileycom/pers”> <name/> <xhtml:p xmlns:xhtml=“http://www.w3.org/1999/xhtml”> This is XHTML </xhtml:p> </person> Example 2 <person xmlns=“http://www.wileycom/pers”> <name/> <p xmlns=“http://www.w3.org/1999/xhtml”>This is XHTML</p> </person>

  9. Namespaces • Look at Example 1 on p 73 of the book. • Then look at example 1 on p 79 • Both documents have the same semantic content

  10. Canceling Default Namespaces <employee> <name>Jane Doe</name> <notes> <p xmlns=“http://www.w3.org/1999/xhtml”>I’ve worked with <name xmlns=““>Jane Doe</name> for over a <em>year</em> now.</p> </notes> </employee>

  11. Do Different Notations Make Any Difference? You can fully qualify every name, like this: <pers:person xmlns:pers="http://www.wiley.com/pers" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <pers:name/> <xhtml:p>This is XHTML</xhtml:p> </pers:person> Or, you can use one namespace as the default, and just qualify any names from other namespaces, like this: <person xmlns="http://www.wiley.com/pers" xmlns:xhtml="http://www.w3.org/1999/xhtml"> <name/> <xhtml:p>This is XHTML</xhtml:p> </person> Or, you can just use defaults everywhere, like this: <person xmlns="http://www.wiley.com/pers"> <name/> <p xmlns="http://www.w3.org/1999/xhtml">This is XHTML</p> </person>

  12. Namespaces and Attributes Example 1 <person xmlns=“http://www.wileycom/pers”> <name id=“25”> <title>Sir</title> </name> </person> Example 2 <a:person xmlns:a=“http://www.wileycom/pers”> <a:name a:id=“25”> <a:title>Sir</a:title> </a:name> </a:person>

  13. Try It Out Namespaces in Action

  14. What Exactly Are URIs? http://www.wileycom/default/home.htm Protocol URI URL = Protocol + URI

  15. URNs urn:foo:a123,456 • Examples • Social security numbers • Serial numbers • NIC mac addresses

  16. Why Use URLs for Namespaces, Not URNs? XML uses URLs not URNs... by the way!

  17. RDDL • Resource Directory Description Language It's the human-readable stuff in the namespaces.

  18. When Should You Use Namespaces? Remember that a namespace is just a “bag of names”—that is, it’s a group of element names that belong together, and that are distinct from element names in other namespaces. The key is the phrase belong together.

  19. Exercise Questions Question 1 In this chapter you saw the following XML document, in which you had to cancel the default namespace: <employee> <name>Jane Doe</name> <notes> <p xmlns="http://www.w3.org/1999/xhtml">I’ve worked with <name xmlns="">Jane Doe</name> for over a <em>year</em> now.</p> </notes> </employee> Assuming that this document is for Wiley’s HR department, create a namespace for employees, and use it in this document. Be sure to keep the XHTML elements in their namespace.

  20. Exercise Questions Question 2 Imagine that Wiley has been going through the employee records and realized that they don’t have a good unique way to identify each employee. Create a global id attribute that can be attached to any XML element in the employee namespace you created earlier. Put this attribute into effect by modifying the XML you created in Question 1, and marking the Jane Doe employee as employee number x125.

  21. Exercise Questions Question 3 Create a new XML file for an employee named Alfred Neuman, with employee number x393. In the notes for Alfred mention that he has worked closely with Jane Doe, being sure to use the <name> element to refer to her.

  22. Try It Out • Adding XML Namespaces to Your Document • Default Namespaces in Action • Default Namespaces for Children • Adding Attributes

More Related