如何在 XSL/XPath 中按名称的一部分选择元素? [英] How to select elements by part of their name in XSL/XPath?

查看:37
本文介绍了如何在 XSL/XPath 中按名称的一部分选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 apply-templates 仅选择以特定模式结尾的名称(而非值)元素?假设下面的xml...

How do I use apply-templates to select only those elements by name (not value) that end with a specific pattern? Assume the following xml...

<report>
  <report_item>
    <report_created_on/>
    <report_cross_ref/>
    <monthly_adj/>
    <quarterly_adj/>
    <ytd_adj/>
  </report_item>
  <report_item>
   ....
  </report_item>
</report>

我想在 的所有实例上使用 ,其中后代元素以 'adj` 结尾,所以,在这个在这种情况下,只有monthly_adj、quaterly_adj 和ytd_adj 会被选择并与模板一起应用.

I want to use <xsl:apply-templates> on all instances of <report_item> where descendant elements end with 'adj`, so, in this case, only monthly_adj, quaterly_adj, and ytd_adj would be selected and applied with the template.

<xsl:template match="report">
   <xsl:apply-templates select="report_item/(elements ending with 'adj')"/>
</xsl:template>

推荐答案

我不认为正则表达式语法在此上下文中可用,即使在 XSLT 2.0 中也是如此.但在这种情况下你不需要它.

I don't think that regular expression syntax is available in this context, even in XSLT 2.0. But you don't need it in this case.

<xsl:apply-templates select="report_item/*[ends-with(name(), 'adj')]"/>

* 匹配任意节点

[pred] 对选择器执行节点测试(在本例中为 *)(其中 pred 是在上下文中评估的谓词所选节点的)

[pred] performs a node test against the selector (in this case *) (where pred is a predicate evaluated in the context of the selected node)

name() 返回元素的标签名称(为此应该等同于 local-name).

name() returns the element's tag name (should be equivalent to local-name for this purpose).

ends-with() 是一个内置的 XPath 字符串函数.

ends-with() is a built-in XPath string function.

这篇关于如何在 XSL/XPath 中按名称的一部分选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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