1 / 12

XML Namespaces

Explore the concept of XML namespaces in web services, learn how to differentiate element types and attribute names using namespaces. Discover the significance and application of namespaces through examples.

marinar
Télécharger la présentation

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. XML Namespaces Kanda Runapongsa (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University

  2. What are Namespaces? • Namespaces are a simple and straightforward way to distinguish names used in XML documents, no matter where they come from • [W3C Definition] An XML namespace is a collection of names, identified by a URI reference, which are used in XML documents as element types and attribute names. 168493: XML and Web Services (II/2546)

  3. <h:html xmlns:xdc="http://abc.com/books" xmlns:h="http://www.w3.org/HTML"> <h:head> <h:title>Book Review</h:title> </h:head> <h:body> <xdc:bookreview> <xdc:title>XML</xdc:title> </xdc:bookreview> </h:body> </h:html> 168493: XML and Web Services (II/2546)

  4. Namespaces by Example • In the example, the elements prefixed with xdc are associated with a namespace whose name is http://abc.com/books • Those prefixed with h are associated with a namespace whose name is http://www.w3.org/HTML 168493: XML and Web Services (II/2546)

  5. Prefixes • Prefixes are linked to the full names using the attributes on the top element whose names begin xmlns: • Prefixes are just shorthand placeholders for the full names • The full names are URLs, i.e. Web addresses 168493: XML and Web Services (II/2546)

  6. Why Namespaces? • Book-review tags and HTML tags are all mixed up together • Need to be sure that we’re finding the book titles, not the HTML page titles • Can be done by processing only <title> tags that are in the http://abc.com/books namespace 168493: XML and Web Services (II/2546)

  7. Namespace Defaulting • Previously, we have all those prefixes and colons cluttering up the tags • We can declare a default namespace and leave out some prefixes • Anything without a prefix is assumed to be in the default namespace • A default namespace is considered to apply to the element where it is declared and to all elements with no prefix 168493: XML and Web Services (II/2546)

  8. <html xmlns:xdc="http://abc.com/books" xmlns="http://www.w3.org/HTML"> <head> <title>Book Review<title> </head> <body> <xdc:bookreview> <xdc:title>XML</xdc:title> </xdc:bookreview> <body> </html> 168493: XML and Web Services (II/2546)

  9. Namespace Defaulting (Cont.) • The default namespace can be set to the empty string • This has the same effect as having no default namespace • Note that default namespaces do not apply directly to attributes 168493: XML and Web Services (II/2546)

  10. Default Namespaces <!--the default namespace is now that of HTML -->  <table xmlns='http://www.w3.org/HTML'>   <th><td>Name</td></th>    <tr>      <!- no default namespace inside table cells -->     <td><brandName xmlns="">Huntsman</brandName></td> … 168493: XML and Web Services (II/2546)

  11. Attributes: Example 1 <x xmlns:n1="http://www.w3.org"    xmlns:n2=“http://www.w3.org”>  <e1  a="1"     a="2" />  <e2 n1:a="1"  n2:a="2" /> </x> • Is the start tag of e1 legal? • Is the start tag of e2 legal? 168493: XML and Web Services (II/2546)

  12. Attributes: Example 2 <x xmlns:n1="http://www.w3.org"    xmlns="http://www.w3.org" >  <e3 a="1"     b="2" />  <e4 a="1"     n1:a="2" /> </x> • Is the start tag of e3 legal? • Is the start tag of e4 legal? 168493: XML and Web Services (II/2546)

More Related