Sie sind auf Seite 1von 7

COSC2104/2106 Document Markup Languages Semester 1, 2014

XML Namespaces

Tutorial Exercises
1. Basic Concepts Answer the questions below. (a) What is a default namespace? Answer: A namespace declared by using the xmlns attribute on an element becomes the default namespace for that element and all child elements which do not have a namespace prex or its own xmlns attribute. An element can contain only one default namespace declaration. Dening a default namespace for an element saves us from using prexes in all the child elements. Default namespace declarations do not apply directly to attribute names. If there is no default namespace declaration, then there is no namespace its just null. To cancel the default namespace, the attribute value in a default namespace declaration MAY be empty, i.e. xmlns="". (b) What is a namespace URI? Answer: A Uniform Resource Identier is a string of characters which identies an Internet Resource. The namespace URI is only used as a name, not a location. It is just a string used to uniquely identify a namespace. (c) What is a namespace prex? Answer: The prex is the name before the colon. This prex is used with each element to indicate to which namespace the element belongs. It does NOT actually identify the namespace. The namespace is identied by the URI. Any prexed element is a qualied name. (d) What is a QName? Answer: QName stands for qualied name namespace prefix:localname. It is an XML name consisting of a prex, a single colon as a separator and the local part (or local name). For example: 1

1: 2: 3:

<doc xmlns:x="http://example.com/ns/foo"> <x:p/> </doc>

In line 2 the prex x is declared to be associated with the URI http://example.com/ns/foo. This prex can further on be used as abbreviation for this namespace. Subsequently the tag x:p is a valid QName because it uses the x as namespace reference and p as local part. The tag doc is also a valid QName, but it consists only of a local part. (e) What is the scope of a namespace declaration? What happens when an XML namespace declaration goes out of scope? Answer: The scope of an XML namespace declaration is that part of an XML document to which the declaration applies. An XML namespace declaration remains in scope for the element on which it is declared and all of its descendants, unless it is overridden or undeclared on one of those descendants. When an XML namespace declaration goes out of scope, it simply no longer applies. (f) Can one use XML namespaces in a DTD? Answer: Yes and no. DTD-based validation is not namespace-aware in the following sense: a DTD constrains the elements and attributes that may appear in a document by their un-interpreted names, not by (namespace name, local name) pairs. To validate a document that uses namespaces against a DTD, the same prexes must be used in the DTD as in the XML instance. 2. Advanced Concepts Answer the following questions: (a) How can XML namespace be declared in an XML document? Answer: To declare an XML namespace, you use an attribute whose name has the form xmlns:prefix OR xmlns. These attributes are often called xmlns attributes and their value is the name of the XML namespace being declared, which is a URI reference. The rst form of the attribute xmlns:prefix declares a prex to be associated with the XML namespace. The second form xmlns declares that the specied namespace is the default XML namespace. (b) Can one use an attribute default in a DTD to declare a namespace? Answer: Yes. For example, the following uses the FIXED attribute xmlns:foo on the A element type to associate the foo prex with the http://www.foo.org/

namespace. The eect of this is that both A and B are in the http://www.foo.org/ namespace. <?xml version="1.0"?> <!DOCTYPE foo:A [ <!ELEMENT foo:A (foo:B)> <!ATTLIST foo:A xmlns:foo CDATA #FIXED "http://www.foo.org/"> <!ELEMENT foo:B (#PCDATA)> ]> <!-- foo prefix declared through default attribute. --> <foo:A> <foo:B>abc</foo:B> </foo:A> (c) How can one override an XML namespace declaration that uses a prex? Answer: To override the prex used in an XML namespace declaration, you simply declare another XML namespace with the same prex. For example, in the following, the foo prex is associated with the http://www.foo.org/ namespace on the A and B elements and the http://www.bar.org/ namespace on the C and D elements. That is, the names A and B are in the http://www.foo.org/ namespace and the names C and D are in the http://www.bar.org/ namespace. <foo:A xmlns:foo="http://www.foo.org/"> <foo:B> <foo:C xmlns:foo="http://www.bar.org/"> <foo:D>abcd</foo:D> </foo:C> </foo:B> </foo:A> In general, this leads to documents that are confusing to read and should be avoided. (d) When should one use a default namespace instead of prexes? Answer: Whether you use the prex or default namespace is a matter of personal preference. There is no signicant performance dierence either way. Sometimes we use a default namespace makes the document clearer and easier to understand. If a default namespace is not used, then a prex must be dened and used on each element. For example, the following 3 fragments mean the same thing! <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> 3

</pers: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> <person xmlns="http://www.wiley.com/pers"> <name/> <p xmlns="http://www.w3.org/1999/xhtml">This is XHTML</p> </person> 3. Application of the Concepts Consider the following XML document: (Example taken from
http://www.zvon.org/xxl/NamespaceTutorial/Output/example14.html)

1: <OOO xmlns="http://zvon.org/xnumber" xmlns:lower="http://zvon.org/lowercase" ooo="222" > 2: <aaa xmlns:lower="http://zvon.org/lowercase" > 3: <lower:BBB xmlns:lower="http://zvon.org/uppercase" > 4: <lower:x111 /> 5: <sss xmlns:lower="http://zvon.org/xnumber" > 6: <lower:x111 /> 7: </sss> 8: </lower:BBB> 9: <lower:x111 /> 10: </aaa> 11: <aaa xmlns="http://zvon.org/uppercase" > 12: <BBB xmlns:upper="http://zvon.org/uppercase" upper:U="A" U="A" lower:U="A" /> 13: </aaa> 14: <aaa xmlns="http://zvon.org/lowercase" > 15: <upper:BBB xmlns:upper="http://zvon.org/uppercase" xmlns="http://zvon.org/xnumber"> 16: <x111 /> 17: </upper:BBB> 18: <x111 /> 19: </aaa> 20: </OOO> Answer the following questions: Identify the default namespace (the value of the xmlns attribute) that has been declared with each of the two x111 elements shown on lines 16 and 18. Is the same default namespace declared with these two elements?

Answer: No. The x111 element shown on line 16 belongs to http://zvon.org/xnumber, whereas the x111 element shown on line 18 belongs to the http://zvon.org/lowercase default namespace. There are three x111 elements shown on lines 4, 6, and 9. Identify the namespace (both the prex and the value of the xmlns attribute) that has been declared with each of these elements. Is the same namespace prex declared with the three elements? Is the same value of the namespace attribute declared with the three elements? Which of the two the prex or the value uniquely identies a namespace? Answer: Yes, the same prex has been declared with the three elements. No, they have three dierent values for the xmlns attribute: http://zvon.org/uppercase, http://zvon.org/xnumber, and http://zvon.org/lowercase, respectively. The value of the xmlns attribute uniquely identies the namespace, not the prex. There are three attributes shown on line 12: upper:U="A", U="A", and lower:U="A". To which namespaces do these attributes belong? Answer: upper:U="A" attribute belongs to http://zvon.org/uppercase, the lower:U="A" to http://zvon.org/lowercase (note: this namespace is attached to the document element), whereas the U="A" attribute DOES NOT belong to any namespace (not even to the default namespace attached to the document element)!

Laboratory Exercises
1. Consider the following document: (Example adapted from http://msdn.microsoft.com/ library) <BOOKS> <bk:BOOK xmlns:bk="http://www.booklovers.org/BookInfo" xmlns:money="http://www.finance.org/Money"> <bk:TITLE>Creepy Crawlies</bk:TITLE> <bk:PRICE money:currency="US Dollar">22.95</bk:PRICE> </bk:BOOK> </BOOKS>

Perform the following activities: Rewrite the above XML document using the default namespace declaration. Answer: <BOOKS> <BOOK xmlns="http://www.booklovers.org/BookInfo" xmlns:money="http://www.finance.org/Money"> 5

<TITLE>Creepy Crawlies</TITLE> <PRICE money:currency="US Dollar">22.95</PRICE> </BOOK> </BOOKS> Add an explicit namespace declaration http://www.booklovers.org/AuthorInfo to the <BOOK> element with a prex of author. Add an element called <AUTHOR> to the above XML document, which should be qualied with the namespace prex author, and will contain the name Stefan Knorr. Answer: <BOOKS> <BOOK xmlns="http://www.booklovers.org/BookInfo" xmlns:money="http://www.finance.org/Money" xmlns:author="http://www.booklovers.org/AuthorInfo"> <TITLE>Creepy Crawlies</TITLE> <PRICE money:currency="US Dollar">22.95</PRICE> <author:AUTHOR>Stefan Knorr</author:AUTHOR> </BOOK> </BOOKS> 2. You have decided to publish your bookmarks in some sort of a portal web and for that purpose you have developed a list of bookmarks in an XML format as bookmarks1.xml: <?xml version = "1.0"?> <bookmarks> <site href="http://abc.com"> <title>You are Ruining My Bad Reputation</title> </site> <site href="http://def.com"> <title>Guess My Eyes Were Bigger than My Heart</title> </site> </bookmarks> It is often desirable to extend documents to convey new information and thus you add a rating element to the above document as bookmarks2.xml: <?xml version = "1.0"?> <bookmarks> <site href="http://abc.com"> <title>You are Ruining My Bad Reputation</title> <rating>5 stars</rating> </site> <site href="http://def.com"> <title>Guess My Eyes Were Bigger than My Heart</title> <rating>4 stars</rating> </site> </bookmarks> 6

Now consider another scenario. Suppose somebody else decided to rate the list, but instead of quality, it rates against family criteria as bookmarks3.xml: <?xml version = "1.0"?> <bookmarks> <site href="http://abc.com"> <title>You are Ruining My Bad Reputation</title> <rating>PG (parental guidance) </rating> </site> <site href="http://def.com"> <title>Guess My Eyes Were Bigger than My Heart</title> <rating>MA 15 (mature audience) </rating> </site> </bookmarks> This is problematic as both the listings bookmarks2.xml and bookmarks3.xml have a rating element. So the two people have extended the same document in two dierent ways. Now you want to combine both these documents and include both the ratings in your XML document as the DTD for this document does not allow you to call both the elements as rating. How will you solve this problem using explicit namespaces? Rewrite the document bookmarks1.xml to include ratings from both bookmarks2.xml and bookmarks3.xml. Answer: bookmarks.xml <?xml version = "1.0"?> <bookmarks xmlns:pg = "http://you.can.cook.up.this.url" xmlns:qa = "http://another.url" xmlns = "https://www.abc.def/bookmark"> <site href="http://abc.com"> <title>You are Ruining My Bad Reputation</title> <qa:rating>5 stars</qa:rating> <pg:rating>PG (parental guidance)</pg:rating> </site> <site href="http://def.com"> <title>Guess My Eyes Were Bigger Than My Heart</title> <qa:rating>4 stars</qa:rating> <pg:rating>MA 15 (mature audience)</pg:rating> </site> </bookmarks>

Das könnte Ihnen auch gefallen