用变量/函数替换 XPath 表达式 [英] Replacing XPath Expression with Variable/Function

查看:28
本文介绍了用变量/函数替换 XPath 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 XSLT 2.0 样式表.我需要执行 for-each-group 循环,但我分组的值可能会根据传递到样式表的参数而改变.

I have an XSLT 2.0 stylesheet. I need to perform a for-each-group loop, but the value I am grouping on may change, based on parameters passed into the stylesheet.

例如,如果我的 XML 看起来像这样(针对此示例进行了简化):

For example, if my XML looks like this (simplified for this example):

<root><row><date>2001-01-01</date><text>abc</text></row><row><date>2003-04-02</date><text>xyz</text></row>...</root>

我想对应用于日期节点的函数执行 for-each-group 分组.例如,它可能是这样的:

I want to perform a for-each-group, grouping on a function applied to the date node. for example, it might be this:

<xsl:for-each-group select="/root/row" group-by="substring(date,1,4)">

或者这个:

<xsl:for-each-group select="/root/row" group-by="substring(date,1,7)">

我什至可能需要在不同的节点上分组,例如 .所以我可以使用:

I might even need to group on a different node, like . So I could use:

<xsl:for-each-group select="/root/row" group-by="$variable">

有什么方法可以用我想要分组的函数和节点定义一个变量,然后在 group-by 表达式中使用该变量?或其他一些方法来做到这一点?我尝试使用一个简单的变量,但这都不起作用(第二个在单引号内):

Is there any way to define a variable with the function and node I want to group on and then use that variable in the group-by expression? or some other way to do this? I tried using a simple variable, but that did not work, both (the second is inside single quotes):

<xsl:variable name="variable" select="substring(date,1,4)"/>

<xsl:variable name="variable" select="'substring(date,1,4)'"/>

我有一个相关的问题,但它可能有相同的答案.我还需要执行一个选择也需要使用相同变量的地方,例如:

I have a related question, but it likely has the same answer. I also need to perform an where the select will also need to use this same variable, like:

<xsl:value-of select="/root/row/$variable"/>

谢谢!

推荐答案

您可以使用 substring(*[local-name() = $var1], 1, $var2) 在那里绑定var1 作为 'date' 和另一个带有数值的变量.

You can use substring(*[local-name() = $var1], 1, $var2) where you then bind var1 as 'date' and the other variable with a number value.

这篇关于用变量/函数替换 XPath 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆