120 likes | 136 Vues
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.
E N D
XML Namespaces Kanda Runapongsa (krunapon@kku.ac.th) Dept. of Computer Engineering Khon Kaen University
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)
<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)
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)
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)
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)
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)
<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)
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)
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)
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)
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)