Recipe 6.3. Modularizing and ModesProblemXSLT 1.0 limitations on use o f modes often resulted in duplication of code or extra effort. SolutionUse XSLT 2.0's new mode attribute's capabilities to eliminate code duplication. Consider a simple example of a stylesheet that uses two different modes to process a document in two passes. In each pass, you would like to ignore text nodes, by default. In XSLT 1.0, you would have to write something like the following: <xsl:template match="text( )" mode="mode1"/> <xsl:template match="text( )" mode="mode2"/> However, in 2.0, you can remove the redundancy: <xsl:template match="text( )" mode="mode1 mode2"/> Or if the intention is to match in all modes: <xsl:template match="text( )" mode="#all"/> Granted, this is a small improvement, but it has a large payback for stylesheets that are more complex, use a large number of modes, share a lot of code between modes, or are under frequent maintenance. DiscussionA rule of thumb that I adhere to when using modes in 2.0 is to always use #current rather than an explicitly named mode if my intention is to continue processing in the present mode: <xsl:template match="author" mode="index"> <div > <xsl:apply-templates mode="#current"/> </div> </xsl:template> This has two immediately beneficial consequences. First, if you later decide you picked a bad name for the mode and want to change it, you will not need to change any of the calls to xsl:apply-templates. Second, if you add new modes to the template, it will continue to work without further change: <xsl:template match="author" mode="index body"> <div > <xsl:apply-templates mode="#current"/> </div> </xsl:template> |