我一直在学习将模式属性与 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"的 apply-templates。
当然,我可以使用 mode="print"创建一个新模板,但随后我必须维护两个模板。
或者也许有更好的方法来做到这一点?谢谢您的帮助。 - 斯科特
最佳答案
目前还没有直接的方法可以做到这一点。一种方法可以是-
<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>
关于xslt - 有没有办法在 xsl :if test? 中使用模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370734/