Search code examples
xsltxslt-2.0mode

Is there a way to use mode in an xsl:if test?


I've been learning to use the mode attribute with XSLT and was wondering if there's a way to test for it within a template, such as in an xsl:if statement? I've only seen it used at the xsl:template level and maybe that's the only way. Say I want to add a "../" in front of a path attribute (@href), but only if mode="print":

     <xsl:template name="object" mode="#all">
        <img>
         <xsl:attribute name="src">
          <xsl:if test="mode='print'"><xsl:text>../</xsl:text></xsl:if>
          <xsl:value-of select="@href"/>
         </xsl:attribute>
        </img>
     </xsl:template>

I'm calling apply-templates with and without the mode="print" set from various other templates.

Of course I can make a new template with mode="print" but then I'd have to maintain two templates.

Or maybe there's a better way to do this? Thanks for the help. - Scott


Solution

  • There is no direct way to do it yet. One approach can be-

    <xsl:template match="/">
    
       <xsl:apply-templates select="something" mode="a">
          <xsl:with-param name="mode" select="'a'" tunnel="yes"/>
       </xsl:apply-templates>
    
       <xsl:apply-templates select="something" mode="b">
          <xsl:with-param name="mode" select="'b'" tunnel="yes"/>
       </xsl:apply-templates>
    
    </xsl:template>
    

    and then in the match-

    <xsl:template match="blah" mode="a b">
       <xsl:param name="mode" tunnel="yes"/>
    
       <xsl:if test="$mode='a'">
         <!-- Do Something -->
       </xsl:if>
    
       <xsl:if test="$mode='b'">
         <!-- Do Something -->
       </xsl:if>
    
    </xsl:template>