Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concat function with strings in XPath

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!

like image 352
Mtaly Avatar asked Aug 15 '16 18:08

Mtaly


People also ask

How to concatenate string in XPath?

The concat operator '||' was introduced in XPath 3.0. It used in string concatenation expressions to concatenate two strings. The 'to' operator is used in sequence expressions to construct a sequence of items of type 'xs:integer' from a range specified by the left and right operands.

What is fn concat?

The XPath 2.0 function fn:concat(arg1, arg2, ,,,) takes two or more arguments, converts them to their string representation, and concatenates them, returning a single string. The fn:concat transform is the representation of the fn:concat XPath function in the Graphical Data Mapping editor.

How do I concatenate strings in XSLT?

Concat function in XSLT helps to concatenate two strings into single strings. The function Concat() takes any arguments that are not the string is been converted to strings. The cardinality function passed over here in each argument should satisfy zero or one function() respectively.


1 Answers

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, ' ')
like image 148
kjhughes Avatar answered Oct 02 '22 11:10

kjhughes