10

I would like to use XPath to extract all the text from all the <li> elements, that are in the specialList list and return one string separated by spaces or commas. Is this possible?

Lets say the DOM includes the following HTML:

<ul class="specialList">
   <li>one</li>
   <li>two</li>
   <li>three</li>
   <li>four</li>
</ul>

Desired Output

one, two, three, four

OR

one two three four

3 Answers 3

10

In XPath 1.0, this is only possible if you know the number of elements in advance using concat(...):

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

If you're lucky, you can just return all result strings for //li/text() and set the output parameters of your XPath processor to concatenate them like you want. This depends on the processor, so there is no general solution and this is no way to go if you want to further process the results within XPath.

In XPath 2.0, you can use fn:string-join($sequence, $delemiter) for input of arbitrary length:

fn:string-join(//li, ', ')
Sign up to request clarification or add additional context in comments.

Comments

2

Though this is possible using XSLT 1.0

<xsl:for-each select="ul/li">
   <xsl:value-of select="."/>
   <xsl:if test="position() != last()">
       <xsl:text>,</xsl:text>
   </xsl:if>
</xsl:for-each>

Comments

0

As Jens says, in XPath version 2 and later, you can use the string-join function to join together an arbitrary number of text nodes, with an arbitrary separator, e.g. here using a comma and a space:

string-join(//li, ', ')

Whereas in XPath 1 an arbitrary separator can only be used if you know in advance how many text nodes you are joining together, and your XPath expression refers to each node individually.

But in XPath 1 you could achieve something similar if you can rely on the presence of white space text nodes in between the li elements (i.e. text nodes which are siblings of the li elements and children of the ul element). Your example has those, but it's possible that just because you wanted to produce a readable example.

normalize-space(/ul)

The result is the string "one two three four"

This expression converts the ul element to a string, and then converts any sequence of white space characters in that string into a single space character.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.