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:
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>
|