如何在XSL/XPath中按部分名称选择元素?

raf*_*ian 6 regex xml xslt transform

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

<report>
  <report_item>
    <report_created_on/>
    <report_cross_ref/>
    <monthly_adj/>
    <quarterly_adj/>
    <ytd_adj/>
  </report_item>
  <report_item>
   ....
  </report_item>
</report>
Run Code Online (Sandbox Code Playgroud)

我想<xsl:apply-templates>在所有子<report_item>元素以'adj`结尾的实例上使用,因此,在这种情况下,只选择monthly_adj,quaterly_adj和ytd_adj并使用模板.

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

har*_*rpo 10

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

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

* 匹配任何节点

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

name()返回元素的标记名称(应该等效local-name于此目的).

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