xsl:next-match

  1. next-match_1-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. </products>
  1. next-match_1-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.     <PRODUCTS>
  7.       <xsl:apply-templates/>
  8.     </PRODUCTS>
  9.   </xsl:template>
  10.   <xsl:template match="product">
  11.     <PRODUCT id="{@id}" price="{@price}" stock="{@stock}"/>
  12.   </xsl:template>
  13.   <xsl:template match="product[@id = 'p2']">
  14.     <PRODUCT id="{@id}" price="{@price * 1.25}" stock="{@stock}"/>
  15.   </xsl:template>
  16.   <xsl:template match="product|product[@id = 'p2']">
  17.     <xsl:comment>
  18.       <xsl:value-of select="concat(' ', @name, ' ')"/>
  19.     </xsl:comment>
  20.     <xsl:text>&#xA;</xsl:text>
  21.     <xsl:next-match/>
  22.     </xsl:template>
  23. </xsl:stylesheet>
  1. next-match_1-output.xml

  2. <?xml version="1.0" encoding="UTF-8"?>
    <PRODUCTS>
      <!-- Delta -->
      <PRODUCT id="p1" price="3250" stock="4"/>
      <!-- Golf -->
      <PRODUCT id="p2" price="1250" stock="5"/>
      <!-- Alpha -->
      <PRODUCT id="p3" price="1200" stock="19"/>
    </PRODUCTS>

Comment

xsl:next-match is new in XSLT 2.0. Let us say that we need to insert a comment before all "product" elements matched by very many templates. We need only one new template creating the comment and then xsl:next-match.

Updated 2009-03-19