Concat function with strings in XPath

Mtaly picture Mtaly · Aug 15, 2016 · Viewed 13k times · Source

I am trying to get the complete address using XPath. I am new to XPath. This is what I have done so far:

<p class="adr" prop1="address">
<span class="street-address" name="streetname">2222 Warnar Ave</span>
<span class="country" name="country">USA, </span>
<span name="State">CA</span>
<span name="zip">1111</span>
</p>

My XPath

/p/span/text()

Result like an array:

Text='2222 Warnar Ave'
Text='USA,'
Text='CA'
Text='1111'

And I want the result as a complete one line address

2222 Warnar Ave USA, CA 1111

I have used concat() in my XPath but nothing is showing!

Answer

kjhughes picture kjhughes · Aug 15, 2016

XPath 1.0 Solution

concat(/p/span[1],' ',/p/span[2],' ',/p/span[3],' ',/p/span[4])

Of course, instead of /p/span[1] you could specify /p/span[@class='street-address'] etc.

XPath 2.0 Solution

string-join(/p/span, ' ')