有没有办法在xsl:if测试中使用模式



我一直在学习将mode属性与XSLT一起使用,并想知道是否有一种方法可以在模板中测试它,例如在xsl:if语句中?我只见过它在xsl:template级别使用,也许这是唯一的方法。假设我想在路径属性(@href)前面添加一个"../",但前提是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>

我调用了应用模板,其中包含和不包含来自其他各种模板的mode="print"集。

当然,我可以用mode="print"制作一个新模板,但之后我必须维护两个模板。

或者也许有更好的方法可以做到这一点?谢谢你的帮助。-Scott

目前还没有直接的方法。一种方法可以是

<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>

然后在比赛中-

<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>

没有办法获得当前模式,但您可以这样做:

<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>

最新更新