Previous | Next | Indexes
Zvon > Tutorials > XSLT 2.0 Tutorial > Context (3/6) >

Dangers of context item "."

When using context item in xsl:apply-templates never ending loops can easily occur. In this example the processing of stylesheet was interrupted by xsl:message instruction with terminate attribute equal "yes". The xsl:message sends its contents to the standard output and the terminate attribute can stop the processing (default value of this attribute is "no").

XSLT

      <xsl:stylesheet
                  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                  version="2.0">
            <xsl:output  method="xml"
                        indent="yes"
                        omit-xml-declaration="yes"/>

            <xsl:template  match="/aaa">
                  <xsl:apply-templates  select="bbb"/>
            </xsl:template>

            <xsl:template  match="bbb">
                  <xsl:param  name="i"
                              select="1"/>
                  <xsl:if  test="$i = 999">
                        <xsl:message  terminate="yes">
                              <xsl:text>FORCED STOP: $i=</xsl:text>
                              <xsl:value-of  select="$i"/>
                        </xsl:message>
                  </xsl:if>
                  <xsl:apply-templates  select=".">
                        <xsl:with-param  name="i"
                                    select="$i + 1"/>
                  </xsl:apply-templates>
            </xsl:template>

      </xsl:stylesheet>
XML

      <aaa>
            <bbb  x="000">B1</bbb>
            <bbb  x="111">B2</bbb>
            <bbb  x="222">B3</bbb>
            <bbb  x="333">B4</bbb>
      </aaa>
Output

Error:


-
FORCED STOP: $i=999 Processing terminated by xsl:message at line L


Previous chapter: Modes
Next chapter: Implicit behaviour
Previous page: Context item . and modes
Next page: Context item in XPath expressions