| Previous | Next
XSLT and NamespacesXPath patterns, as well as expressions that match and select elements, identify these elements based on their local part and namespace URI. They do not consider the namespace prefix. Most commonly, the same namespace prefix is mapped to the same URI in both the input XML document and the stylesheet. However, this is not required. For instance, consider Example 8-14. This is exactly the same as Example 8-1, except that now all the elements have been placed in the namespace http://www.cafeconleche.org/namespaces/people. Example 8-14. An XML document describing two people that uses a default namespace<?xml version="1.0"?> <people xmlns="http://www.cafeconleche.org/namespaces/people"> <person born="1912" died="1954"> <name> <first_name>Alan</first_name> <last_name>Turing</last_name> </name> <profession>computer scientist</profession> <profession>mathematician</profession> <profession>cryptographer</profession> </person> <person born="1918" died="1988"> <name> <first_name>Richard</first_name> <middle_initial>M</middle_initial> <last_name>Feynman</last_name> </name> <profession>physicist</profession> <hobby>Playing the bongoes</hobby> </person> </people> Except for the built-in template rules, none of the rules in this chapter so far will work on this document! For instance, consider this template rule from Example 8-8: <xsl:template match="name"> <p><xsl:value-of select="last_name"/>, <xsl:value-of select="first_name"/></p> </xsl:template> It's trying to match a Example 8-15. An XSLT stylesheet for input documents using the http://www.cafeconleche.org/namespaces/people<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:pe="http://www.cafeconleche.org/namespaces/people"> <xsl:template match="pe:people"> <html> <head><title>Famous Scientists</title></head> <body> <xsl:apply-templates/> </body> </html> </xsl:template> <xsl:template match="pe:name"> <p><xsl:value-of select="pe:last_name"/>, <xsl:value-of select="pe:first_name"/></p> </xsl:template> <xsl:template match="pe:person"> <xsl:apply-templates select="pe:name"/> </xsl:template> </xsl:stylesheet> The output is essentially the same output you get by applying Example 8-8 to Example 8-1 except that it will have an extra |