xml 在 xpath 中连接多个节点值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21996965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Concatenate multiple node values in xpath
提问by krishna2642
I have a XML that looks like this
我有一个像这样的 XML
<element1>
<element2>
<element3>
<element4>Hello</element4>
<element5>World</element5>
</element3>
<element3>
<element4>Hello2</element4>
<element5>World2</element5>
</element3>
<element3>
<element4>Hello3</element4>
<element5>World3</element5>
</element3>
</element2>
</element1>
I am trying to use Xpath to get a result like this:
我正在尝试使用 Xpath 来获得这样的结果:
Hello.World
Hello2.World2
Hello3.World3
I used concat function below but I did not get correct result.
我在下面使用了 concat 函数,但没有得到正确的结果。
Concat function:
连接功能:
concat(/element1/element2/element3/element4/text(),".", /element1/element2/element3/element5/text())
Result I got:
结果我得到:
Hello.World
How can I get the correct result? I am using XPath with Camel Spring DSL.
我怎样才能得到正确的结果?我在 Camel Spring DSL 中使用 XPath。
Edit:
编辑:
Solutions in XQuery, XSLT and SPel are also appreciated.
XQuery、XSLT 和 SPel 中的解决方案也很受欢迎。
Edit
编辑
I tried string-join and it did not work:
我试过字符串连接,但没有用:
string-join function:
字符串连接函数:
string-join((/element1/element2/element3/element4/text(), /element1/element2/element3/element5/text()),".")
Result I got:
结果我得到:
Hello.Hello2.Hello3.World.World2.World3
回答by Tim C
Try this expression...
试试这个表情...
string-join(//element3/(concat(element4/text(), '.', element5/text())), " ")
回答by prespic
I used concat method and works well.
我使用了 concat 方法并且效果很好。
concat(//SomeElement/text(),'_',//OtherElement/text())
回答by hek2mgl
Here comes a solution with XSLT:
这里有一个解决方案XSLT:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//element3">
<xsl:value-of select="element4/text()" />.<xsl:value-of select="element5/text()" />
</xsl:template>
</xsl:stylesheet>
回答by bosari
for $d in $doc/element2/element3
return fn:string-join(fn:data($d/element()), ".").
$doc stores the Xml.
for $d in $doc/element2/element3
return fn:string-join(fn:data($d/element()), ".").
$doc 存储 Xml。
回答by VISHNU SINGH
<xsl:template match="element3">
<xsl:value-of select="element4,element5" separator="."/>
</xsl:template>

