xsl:namespace-alias

  1. namespace-alias_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <name>Alpha</name>
  5.   <name>Bravo</name>
  6.   <name>Charlie</name>
  7.   <name>Delta</name>
  8. </products>
  1. namespace-alias_1-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4.   xmlns:jt="http://www.xmlplease.com/local" xmlns:please="http://www.xmlplease.com/ns1">
  5.   <xsl:output indent="yes"/>
  6.   <xsl:namespace-alias stylesheet-prefix="jt" result-prefix="please"/>
  7.   <xsl:template match="/">
  8.     <jt:products>
  9.       <xsl:for-each select="products/name">
  10.         <jt:name>
  11.           <xsl:value-of select="."/>
  12.         </jt:name>
  13.       </xsl:for-each>
  14.     </jt:products>
  15.   </xsl:template>
  16. </xsl:stylesheet>
  1. namespace-alias_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <please:products xmlns:please="http://www.xmlplease.com/ns1">
  4.   <please:name>Alpha</please:name>
  5.   <please:name>Bravo</please:name>
  6.   <please:name>Charlie</please:name>
  7.   <please:name>Delta</please:name>
  8. </please:products>

Comment

Let us say we have a huge XSLT stylesheet outputting to the xmlns:jt=http://www.xmlplease.com/ns1 namespace. We want to change the prefix to "please". Maybe only for one run of the stylesheet. The easy way is to rename the original namespace (above it is renamed to "local"), make a new namespace declaration with the new prefix and use the xsl:namespace-alias element to rename the old prefix to the new.

Updated 2009-03-19