xsl:namespace (1)

  1. namespace_1-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" price="1000"/>
  5.   <product id="p2" price="1200"/>
  6.   <product id="p3" price="1250"/>
  7. </products>
  1. namespace_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:x="someNamespace">
  5.   <xsl:output indent="yes"/>
  6.   <xsl:template match="products">
  7.     <xsl:element name="catalog">
  8.       <xsl:namespace name="x" select="'someNamespace'"/>
  9.       <xsl:apply-templates/>
  10.     </xsl:element>
  11.   </xsl:template>
  12.   <xsl:template match="product">
  13.     <xsl:element name="x:{name()}">
  14.       <xsl:attribute name="id" select="@id"/>
  15.       <xsl:attribute name="price" select="@price"/>
  16.     </xsl:element>
  17.   </xsl:template>
  18. </xsl:stylesheet>
  1. namespace_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <catalog xmlns:x="someNamespace">
  4.   <x:product id="p1" price="1000"/>
  5.   <x:product id="p2" price="1200"/>
  6.   <x:product id="p3" price="1250"/>
  7. </catalog>

Comment

In the above example xsl:namespace is only used for cosmetic reasons in order to transfer the namespace declaration from each child element to outermost element. See my tutorial xsl:namespace in XSLT 2.0

Updated 2009-03-19