[xml] Concatenate multiple node values in xpath

I have a XML that looks like this

<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:

Hello.World
Hello2.World2
Hello3.World3

I used concat function below but I did not get correct result.

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.

Edit:

Solutions in XQuery, XSLT and SPel are also appreciated.

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

This question is related to xml xslt xpath xquery spring-el

The answer is


for $d in $doc/element2/element3 return fn:string-join(fn:data($d/element()), ".").
$doc stores the Xml.


<xsl:template match="element3">
        <xsl:value-of select="element4,element5" separator="."/>
    </xsl:template>

If you need to join xpath-selected text nodes but can not use string-join (when you are stuck with XSL 1.0) this might help:

<xsl:variable name="x">
    <xsl:apply-templates select="..." mode="string-join-mode"/>
</xsl:variable>
joined and normalized: <xsl:value-of select="normalize-space($x)"/>

<xsl:template match="*" mode="string-join-mode">
    <xsl:apply-templates mode="string-join-mode"/>
</xsl:template>    

<xsl:template match="text()" mode="string-join-mode">
    <xsl:value-of select="."/>
</xsl:template>    

I used concat method and works well.

concat(//SomeElement/text(),'_',//OtherElement/text())

Here comes a solution with 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>

Examples related to xml

strange error in my Animation Drawable How do I POST XML data to a webservice with Postman? PHP XML Extension: Not installed How to add a Hint in spinner in XML Generating Request/Response XML from a WSDL Manifest Merger failed with multiple errors in Android Studio How to set menu to Toolbar in Android How to add colored border on cardview? Android: ScrollView vs NestedScrollView WARNING: Exception encountered during context initialization - cancelling refresh attempt

Examples related to xslt

Generic XSLT Search and Replace template Concatenate multiple node values in xpath XSL if: test with multiple test conditions Error: The processing instruction target matching "[xX][mM][lL]" is not allowed Weird behavior of the != XPath operator Declaring a xsl variable and assigning value to it How to implement if-else statement in XSLT? How do I specify "not equals to" when comparing strings in an XSLT <xsl:if>? How to concat a string to xsl:value-of select="...? XPath with multiple conditions

Examples related to xpath

Xpath: select div that contains class AND whose specific child element contains text XPath: difference between dot and text() How to set "value" to input web element using selenium? How to click a href link using Selenium XPath: Get parent node from child node What is the difference between absolute and relative xpaths? Which is preferred in Selenium automation testing? How to use XPath preceding-sibling correctly Selenium and xPath - locating a link by containing text How to verify an XPath expression in Chrome Developers tool or Firefox's Firebug? Concatenate multiple node values in xpath

Examples related to xquery

Concatenate multiple node values in xpath How can I query a value in SQL Server XML column Is there a JSON equivalent of XQuery/XPath?

Examples related to spring-el

Thymeleaf using path variables to th:href Concatenate multiple node values in xpath