Xml xpath选择以foo结尾的所有属性?
XPATH/XSLT1.0是否可以选择以“Foo”结尾的所有属性 我正在编写一些XSLT来获取所有“InterestingElement”的所有属性的所有值的列表,其中属性名以“Foo”结尾 实际上,我还想过滤掉那些值为空的 我尝试为XSLT2.0指定样式表,但得到了Xml xpath选择以foo结尾的所有属性?,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,XPATH/XSLT1.0是否可以选择以“Foo”结尾的所有属性 我正在编写一些XSLT来获取所有“InterestingElement”的所有属性的所有值的列表,其中属性名以“Foo”结尾 实际上,我还想过滤掉那些值为空的 我尝试为XSLT2.0指定样式表,但得到了xsl:version:只支持1.0功能: <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xsl:version:只支持1.0功能
:
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fn="http://www.w3.org/2005/xpath-functions"
xmlns:xdt="http://www.w3.org/2005/xpath-datatypes">
到目前为止,我已经:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes"/>
<xsl:template match="/">
<xsl:for-each select="//InterestingElement">
<xsl:value-of select="__what goes here?__"/><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:text>end</xsl:text>
</xsl:template>
</xsl:stylesheet>
结束
下面是一些示例XML:
<?xml version="1.0"?>
<root>
<Other Name="Bob"/>
<InterestingElements>
<InterestingElement AttrFoo="want this"
Attr2Foo="this too"
Blah="not this"
NotThisFoo=""/>
</InterestingElements>
</root>
XPath 2.0解决方案
XPath2.0本身就可以解决这个问题;在XSLT2.0中,不需要xsl:for-each
——只需要xsl:value-of
这个XPath
string-join(//InterestingElement/@*[ends-with(name(.), 'Foo') and . != ''], ' ')
将返回名称以Foo
结尾的所有InterestingElement
属性的(非空)值的空格分隔列表
字符串连接
和以
结尾是XPath 2.0。对于1.0,可以使用以下命令返回所有以空格分隔的值:
<xsl:for-each select="//InterestingElement/@*['Foo' = substring(name(.), string-length(name(.)) - string-length('Foo') +1) and . != '']">
<xsl:value-of select="." /><xsl:text> </xsl:text>
</xsl:for-each>
要获取匹配的属性名称而不是值,请将选择更改为select=“name(.)”
基于和。XSLT 1.0或2.0?您是否也可以共享
xml
sample、所需的输出和您已经尝试过的代码?@user3735178我忘了字符串连接是XSLT 2.0。。。我编辑了我的答案,把它放在一个
中。可能是
或
,但我不是100%确定。或者你也可以删除字符串-join()
并使用@分隔符:
@MadsHansen:好主意。