如何使用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>

我想在后代元素以“adj”结尾的所有<xsl:apply-templates>实例上使用<report_item>,因此,在这种情况下,将仅选择monthly_adj,quaterly_adj和ytd_adj并将其与模板一起应用。
<xsl:template match="report">
   <xsl:apply-templates select="report_item/(elements ending with 'adj')"/>
</xsl:template>

最佳答案

我认为即使在XSLT 2.0中,正则表达式语法也无法在这种情况下使用。但是在这种情况下,您不需要它。

<xsl:apply-templates select="report_item/*[ends-with(name(), 'adj')]"/>
*匹配任何节点
[pred]对选择器执行节点测试(在这种情况下为*)(其中pred是在所选节点的上下文中评估的谓词)
name()返回元素的标签名称(为此,应等效于local-name)。
ends-with()是内置的XPath字符串函数。

关于xml - 如何在XSL/XPath中按元素名称的一部分选择元素?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9267176/

10-12 01:20
查看更多