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

不羁岁月 提交于 2021-02-05 05:24:26

问题


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


回答1:


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>



回答2:


There's no way to get the current mode, but you can do this:

<xsl:template match="object" mode="#all">
  <xsl:param name="print" select="false()"/>

  <!-- Your code here -->
</xsl:template>

<xsl:template match="object" mode="print">
  <xsl:next-match>
    <xsl:with-param name="print" select="true()"/>
  </xsl:next-match>
</xsl:template>


来源:https://stackoverflow.com/questions/26370734/is-there-a-way-to-use-mode-in-an-xslif-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!