有没有办法在 xsl:if 测试中使用模式? [英] Is there a way to use mode in an xsl:if test?
问题描述
我一直在学习在 XSLT 中使用 mode 属性,并且想知道是否有一种方法可以在模板中测试它,例如在 xsl:if 语句中?我只看到它在 xsl:template 级别使用,也许这是唯一的方法.假设我想在路径属性(@href)前面添加一个../",但前提是 mode="print":
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>
我正在调用带有和不带有来自其他各种模板的 mode="print" 集的 apply-templates.
I'm calling apply-templates with and without the mode="print" set from various other templates.
当然我可以用 mode="print" 创建一个新模板,但是我必须维护两个模板.
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
推荐答案
目前还没有直接的方法.一种方法可以是-
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>
然后在比赛中-
<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:if 测试中使用模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!