如何在xsl:apply-templates中使用XSL变量?

Col*_*len 9 xslt

我对xsl:apply-templates的调用相当复杂:

<xsl:apply-templates select="columnval[@id 
                                       and not(@id='_Name_') 
                                       and not(@id='Group') 
                                       and not(@id='_Count_')]"/>
Run Code Online (Sandbox Code Playgroud)

该表达式在其他地方重用,如下所示:

<xsl:apply-templates select="someothernode[@id 
                                           and not(@id='_Name_') 
                                           and not(@id='Group') 
                                           and not(@id='_Count_')]"/>
Run Code Online (Sandbox Code Playgroud)

我想以某种方式概括它,所以我可以定义一次并在其他地方重用它.但是,这似乎不起作用:

<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]"/>
Run Code Online (Sandbox Code Playgroud)

有没有更好/不同的方式这样做?我想要的是在xsl:apply-templates(其中一些从不同的子节点中选择)的多个不同调用中重用xpath表达式.

这将在客户端应用程序中使用,因此我不能使用任何扩展或切换到XSLT 2.:(

谢谢.

Rob*_*ney 5

您无法在XSLT中动态构造XPath(至少不是XSLT 1.0).但是您可以使用模板模式轻松完成您尝试执行的操作:

<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>
Run Code Online (Sandbox Code Playgroud)