Whats in a Name(space)?

 
xslt for dummies
Chapter 3 - Transforming with Style (Stylesheets, That Is)
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002
  

Whats in a Name (space)?

Youve probably noticed that something has come before each of the XSLT elements you have seen and worked with so far in this book. Yes, that xsl: prefix is part of what is known as a namespace. XML uses namespaces to distinguish one set of element names from another set.

The necessity for namespaces becomes clear when you think about the flexibility of the XML language. Because XML has no predefined set of element names, naming is totally up to each developer, whether he or she lives in San Jose, Lucerne, or Ouagadougou. So the possibility of two different developers using an identical element name is very high.

For example, suppose a satellite dish company develops an XML vocabulary using dish as an element, while a housewares company has its own dish element to describe the round thing off which you eat. Now, if these companies never exchange data with anyone outside their companies, they can use their XML vocabulary as is and dont need to use a namespace. But if they wish to exchange data with outside suppliers, the possibility of duplicate element names exists when this data is merged.

Namespaces were developed to avoid this name collision by linking a namespace identifier with a URI (Uniform Resource Identifier). A URI is the method in which resources on the Web are named, the most widespread form being URLs (Uniform Resource Locators), also known simply as Web addresses (for example, http://www.dummies.com ). Because URIs are unique, you can be sure that the namespace associated with the URI is one of a kind.

When you define a namespace, you declare a URI once in a document and then refer to the namespace elsewhere in the document by using a namespace prefix (also known as namespace identifier or abbreviation), as shown in Figure 3-2.


Figure 3-2: A URI is linked to a namespace prefix.

Although you arent required to use namespaces as part of an XML document, you must use them as part of an XSLT stylesheet. If you dont use namespaces in your stylesheet, for example, it would be impossible to tell if you wanted to use <key> to describe the XSLT key element or to refer to an item in a hardware store inventory.

XSLT stylesheets use the URI http://www.w3.org/1999/XSL/Transform and, by convention, assign this URI to an xsl: namespace identifier, as shown in Figure 3-3.


Figure 3-3: XSLT stylesheet namespace.

The mapping from the namespace identifier to the URI is what is important, not the literal namespace identifier xsl: . The xsl: identifier can actually be any label you choose. For example, the following is a perfectly valid XSLT stylesheet:

 <richsmostexcellenttransform:stylesheet xmlns:richsmostexcellenttransform="http://www.w3.o rg/1999/XSL/Transform" version="1.0"> <richsmostexcellenttransform:template match="book"> <p><richsmostexcellenttransform:apply-templates/></p> </richsmostexcellenttransform:template> </richsmostexcellenttransform:stylesheet> 

 Tip   Although you are free to use any prefix you like, I recommend sticking with xsl: . It is the prefix youll see everywhere else. Using an alternative could easily lead to confusion.

  
 
 
2000-2002    Feedback


XSLT For Dummies
XSLT for Dummies
ISBN: 0764536516
EAN: 2147483647
Year: 2002
Pages: 148

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net