如何在 xsl:apply-templates 中使用 XSL 变量? [英] How to use XSL variable in xsl:apply-templates?
问题描述
我对 xsl:apply-templates 的调用相当复杂:
I have a reasonably complex call to xsl:apply-templates:
<xsl:apply-templates select="columnval[@id
and not(@id='_Name_')
and not(@id='Group')
and not(@id='_Count_')]"/>
该表达式在其他地方重复使用,如下所示:
The expression is reused in other places like this:
<xsl:apply-templates select="someothernode[@id
and not(@id='_Name_')
and not(@id='Group')
and not(@id='_Count_')]"/>
我想以某种方式概括它,所以我可以定义一次并在其他地方重用它.但是,这似乎不起作用:
I want to generalize it somehow, so I can define it once and reuse it elsewhere. However, this doesn't seem to work:
<xsl:variable name="x">@id and not(@id='_Name_') and not(@id='Group') and not(@id='_Count_')</xsl:variable>
<xsl:apply-templates select="columnval[$x]"/>
<xsl:apply-templates select="someothernode[$x]"/>
有没有更好/不同的方法来做到这一点?我想要的只是在对 xsl:apply-templates 的多个不同调用中重用 xpath 表达式(其中一些从不同的子项中选择).
Is there a better / different way of doing this? All I want is to reuse the xpath expression in multiple different calls to xsl:apply-templates (some of which select from different children).
这将在客户端应用程序中使用,因此很遗憾我无法使用任何扩展或切换到 XSLT 2.:(
This is going to be used in a client application, so I can't use any extensions or switch to XSLT 2 unfortunately. :(
谢谢.
推荐答案
您不能在 XSLT(至少,不是 XSLT 1.0)中动态构造 XPath.但是您可以使用模板模式轻松完成您想要做的事情:
You can't construct XPath dynamically in XSLT (at least, not XSLT 1.0). But you can easily accomplish what you're trying to do using template modes:
<xsl:apply-templates select="columnval" mode="filter"/>
<xsl:apply-template select="someothernode" mode="filter"/>
...
<!-- this guarantees that elements that don't match the filter don't get output -->
<xsl:template match="*" mode="filter"/>
<xsl:template match="*[@id and not(@id='_Name_') and not(@id='Group') and not(@id='_Count_')]" mode="filter">
<xsl:apply-templates select="." mode="filtered"/>
</xsl:template>
<xsl:template match="columnval" mode="filtered">
<!-- this will only be applied to the columnval elements that pass the filter -->
</xsl:template>
<xsl:template match="someothernode" mode="filtered">
<!-- this will only be applied to the someothernode elements that pass the filter -->
</xsl:template>
这篇关于如何在 xsl:apply-templates 中使用 XSL 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!