xsl:apply-templates (2)

  1. apply-templates_2-input.xml

  2. <?xml version="1.0"?>
  3. <products>
  4.   <product id="p1" name="Delta" price="3250" stock="4"/>
  5.   <product id="p2" name="Golf" price="1000" stock="5"/>
  6.   <product id="p3" name="Alpha" price="1200" stock="19"/>
  7.   <product id="p4" name="Foxtrot" price="1500" stock="5"/>
  8.   <product id="p5" name="Tango" price="1225" stock="3"/>
  9. </products>
  1. apply-templates_2-stylesheet.xsl

  2. <?xml version="1.0"?>
  3. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  4.   <xsl:output indent="yes"/>
  5.   <xsl:template match="*">
  6.     <xsl:element name="{upper-case(name(.))}">
  7.       <xsl:apply-templates select="@*|*"/>
  8.     </xsl:element>
  9.   </xsl:template>
  10.   <xsl:template match="@*">
  11.     <xsl:copy/>
  12.   </xsl:template>
  13.   <xsl:template match="@name">
  14.     <xsl:attribute name="label"><xsl:copy/></xsl:attribute>
  15.   </xsl:template>
  16.   <xsl:template match="product[@id = 'p4']"/>
  17. </xsl:stylesheet>
  1. apply-templates_2-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
    <PRODUCTS>
  3.   <PRODUCT id="p1" label="Delta" price="3250" stock="4"/>
  4.   <PRODUCT id="p2" label="Golf" price="1000" stock="5"/>
  5.   <PRODUCT id="p3" label="Alpha" price="1200" stock="19"/>
  6.   <PRODUCT id="p5" label="Tango" price="1225" stock="3"/>
  7. </PRODUCTS>

Comment

The upper-case() function is new in XSLT 2.0. In XSLT 1.0 we can use tranlate().

Updated 2009-03-19