Book HomeXML in a Nutshell

8.10. XSLT and Namespaces

XPath 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 name element in no namespace, but the name elements in Example 8-13 aren't in no namespace. They're in the http://www.cafeconleche.org/namespaces/people namespace. This template rule no longer applies. To make it fit, we map the prefix pe to the namespace URI http://www.cafeconleche.org/namespaces/people. Then instead of matching name, we match pe:name. That the input document doesn't use the prefix pe is irrelevant as long as the namespace URIs match up. Example 8-15 demonstrates by rewriting Example 8-8 to work with Example 8-14 instead.

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 xmlns:pe attribute on the root element.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.