tags:

views:

31

answers:

2

I am using Xpath to find a list of strings in an HTML document. The strings appear when you type into a text box, to suggest possible results - in other words, it's auto-complete. The problem is, I'm trying to retrieve the whole list of auto-complete suggestions, the results are all split up by <strong> tags.

To give a couple examples: I start typing "str" and the HTML will look like this:

<strong>str</strong>ing

But it gets better! If I don't type anything at all, every single character in the auto-complete results will be interrupted with opening and closing strong tags. Like so:

s <strong></strong> t <strong></strong> r <strong></strong> i <strong></strong> n <strong></strong> g

So, my question is, how do I construct an xpath that retrieves this string, but omits the strong tags?

For reference, the hierarchy of the HTML looks like this:

-div
--ul
---li
----(string I'm looking for)
---li
----(another string I'm looking for)

So my xpath at this point is: //div[@class='class']/ul/li/text(), which will get me the individual parts of the strings.

A: 

That is much less code on the question than people would like to see around here.

But why don't you try a variant like this:

//div[@class='class']/ul/li/strong/text()
Floyd Pink
+1  A: 

This XPath expression:

string(PathToYourDiv/ul/li[$n])

evaluates to the string value of $n-th li child of the ul that is a child of YourDiv. And this is the concatenation of all the text-node descendents od this li element -- effectively giving you the complete string you want.

You have just to substitute YourDiv and $n with specific expressions.

Do not use the // abbreviation, because:

  1. Its evaluation can be very slow.

  2. Indexing such an expression with [] in not intuitive and produces surprizing results that result in a FAQ.

Dimitre Novatchev
@Dimitre: +1 Good answer, explanation about string value, and foresee next question... ;)
Alejandro