Speaking the Namespace Lingo

xslt for dummies
Chapter 15 - Namespaces Revisited
XSLT For Dummies
by Richard Wagner
Hungry Minds 2002

XML namespaces have their own lingo to describe the parts that make up namespaces and element names . In Chapter 3, I talk about the namespace URI and the associated prefix, but other terms, such as QName, NCName, local name, and expanded name , also come into play when working with namespaces. Knowing the lingo gives you a better understanding of namespaces, but it also comes in handy when your mother-in-law asks you about a problem shes having with her QName.

Consider the following XML snippet:

 <nfl:teams xmlns:nfl="http://www.nfl.com"> <nfl:broncos></nfl:broncos> <broncos></broncos> </nfl:teams> 

I can define several namespace- related terms from this XML code, including the following:

  • QName: In this example, nfl:teams and nfl:broncos are two examples of QNames. A QName is a qualified name that consists of an optional namespace prefix and colon and a required local part. The nfl:broncos QName includes nfl as the namespace prefix and broncos as its local part. The broncos element, which has no defined namespace, has a QName of simply broncos .

  • NCName: An NCName (No-Colon Name) is a generic term used to describe the name of either the element or namespace prefix, minus the colon. An NCName must begin with either an alphabetical character or underscore . In the nfl:broncos QName, both nfl and broncos are considered NCNames.

  • Local name: The local name or local part is the name of the element minus any namespace prefix. Both nfl:broncos and broncos have local names of broncos .

  • Expanded-name: An expanded-name consists of a local part name and its companion namespace URI. For example, the expanded name of nfl:broncos is considered the combination of the broncos element and the http://www.nfl.com URI.

 Remember   Dont confuse the term expanded-name with a QName that has both the namespace prefix and local part defined. An expanded name includes the actual namespace URI, not the namespace prefix.

Showing these terms in action, the following stylesheet lists the QName, local name, and namespace URI of the two broncos elements in an HTML table. To do so, the stylesheet uses the XPath functions name() , local-name() , and namespace-uri() :

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nfl="http://www.nfl.com"> <xsl:output method="html"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="nfl:broncosbroncos"> <table border="1"> <tr> <td>QName</td> <td><xsl:value-of select="name()"/></td> </tr> <tr> <td>Local Name</td> <td><xsl:value-of select="local-name()"/></td> </tr> <tr> <td>Namespace URI</td> <td> <xsl:if test="namespace-uri()=''"> null </xsl:if> <xsl:if test="namespace-uri()!=''"> <xsl:value-of select="namespace-uri()"/> </xsl:if> </td> </tr> </table> </xsl:template> </xsl:stylesheet> 

The HTML generated is shown here:

 <html xmlns:nfl="http://www.nfl.com"> <body> <table border="1"> <tr> <td>QName</td> <td>nfl:broncos</td> </tr> <tr> <td>Local Part</td> <td>broncos</td> </tr> <tr> <td>Namespace URI</td> <td>http://www.nfl.com</td> </tr> </table> <table border="1"> <tr> <td>QName</td> <td>broncos</td> </tr> <tr> <td>Local Part</td> <td>broncos</td> </tr> <tr> <td>Namespace URI</td> <td> null </td> </tr> </table> </body> </html> 
2000-2002    Feedback

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

Similar book on Amazon

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