Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何在XSL/XPath中按名称的一部分选择元素?_Xml_Regex_Xslt_Transform - Fatal编程技术网

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

Xml 如何在XSL/XPath中按名称的一部分选择元素?,xml,regex,xslt,transform,Xml,Regex,Xslt,Transform,如何使用应用模板仅按名称(而不是值)选择以特定模式结尾的元素?假设以下xml <report> <report_item> <report_created_on/> <report_cross_ref/> <monthly_adj/> <quarterly_adj/> <ytd_adj/> </report_item> <report_item

如何使用应用模板仅按名称(而不是值)选择以特定模式结尾的元素?假设以下xml

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

....
我想在后代元素以“adj”结尾的所有
实例上使用
,因此,在这种情况下,只会选择每月、季度和年初至今的adj并与模板一起应用

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

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

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

*
匹配任何节点

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

name()

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

稍微简洁一点的解决方案(仅限XSLT 2.0+):


这里有一个自我测试来证明它是有效的。(在撒克逊人身上测试)


简
Q1
2012

新的STACKOVERFLOW世界纪录…最快的答案,哈哈,你说得对,不是正则表达式,只是简单的
结尾就足够了,谢谢。很高兴提供帮助:)我更新了标题以更好地匹配问题。请注意,xslt 1.0中不提供以结尾。有关1.0解决方案,请参阅。
匹配
对我有效。但我有一个问题:(我想按照
mOrder
的顺序说出我想显示的第一、第二、第三个…)谢谢。或者类似的东西:(我希望显示父LI和子LI,但每次迭代和复制都会倍增。我不想显示的空LI,只有在有文本时才显示。)感谢帮助发布问题,问题将得到解答。
<xsl:apply-templates select="report_item/*[matches(name(),'adj$')]"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:fn="http://www.w3.org/2005/xpath-functions" 
                xmlns:xs="http://www.w3.org/2001/XMLSchema" 
                exclude-result-prefixes="xs fn">
 <xsl:output method="xml" indent="yes" encoding="utf-8" />
 <xsl:variable name="report-data">
  <report>
   <report_item>
     <report_created_on/>
     <report_cross_ref/>
     <monthly_adj>Jan</monthly_adj>
     <quarterly_adj>Q1</quarterly_adj>
     <ytd_adj>2012</ytd_adj>
   </report_item>
  </report>
 </xsl:variable>
 <xsl:template match="/" name="main">
  <reports-ending-with-adj>
   <xsl:element name="using-regex">
    <xsl:apply-templates select="$report-data//report_item/*[fn:matches(name(),'adj$')]"/> 
   </xsl:element>
   <xsl:element name="using-ends-with-function">
    <xsl:apply-templates select="$report-data//report_item/*[fn:ends-with(name(), 'adj')]"/>
   </xsl:element>
  </reports-ending-with-adj>
 </xsl:template>
</xsl:stylesheet>