Two levels of contains in an XPath - xpath

I have this XPath:
//tr[contains(td, 'Europe')]
which was working when I had this:
<tr>
<td></td>
<td>Europe</td>
<td></td>
</tr>
but now I have this:
<tr>
<td></td>
<td><a>Europe</a></td>
<td></td>
</tr>
How can I get with an XPath now (based on the fact that Europe is in there).
I tried:
//tr[contains(a, "Europe")]
and
//tr[contains(text(), "Europe")]
and many other silly things without any success.

//tr[contains(td, 'Europe')]
This should work with both schema because fn:contains() cast both arguments to strings.
I do see a problem with a different schema where there can be more than one td element. For that case you should use:
//tr[td[contains(.,'Europe')]]

Related

XPath: Getting a node by attribute value of subnode

People, could you please help me with this XPATH. Lets say I have the following HTML code
<table>
<tr>
<td class="clickable">text</td>
<td>value1</td>
</tr>
<tr>
<td>value2</td>
<td>text</td>
</tr>
</table>
I need to build a XPath that will pick <tr>that have <td> with value text AND attribute class equals clickable.
I tried the following xpath:
//tr[contains(.,'text')][contains(./td/#class,'clickable')]
//tr[contains(.,'text')][contains(td/#class,'clickable')]
but none of those worked
Any help is appreciated
Thanks
You are almost there:
//tr[contains(td/#class,'clickable') and contains(td, 'text')]
Demo using xmllint:
$ xmllint input.xml --xpath "//tr[contains(td/#class,'clickable') and contains(td, 'text')]"
<tr>
<td class="clickable">text</td>
<td>value1</td>
</tr>
If you find tr with a td having value text and a td (maybe, another) with attribute class equals clickable, use answer of #alecxe.
If that is one td with two condition then
//tr[td[.='text' and #class='clickable']]

XPath: returning the index of specific tag inside a set of tags with the same type

Here is an excerpt of my xml:
<table>
...
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
I know how to find specific <tr> tag.
Is it possible to define <tr> tag index or ordinal number inside the <tbody> tag? I guess, that it's possible to loop through the table, but the table is quite large and it will take lots of time.
Is it possible to get this index/ordinal number with single XPATH statement?
I've used following XPath expression:
//tbody//td[text()='findMe']/../following-sibling::tr
These expression calculates, how many 'tr' nodes are located under the node with 'findMe' text. Actually, it useful, because quantity of 'tr' nodes could be obtained.
But, prior to given XPath, a verification should be made, because in case 'finMe' string would be absent, XPath would return 0. The following expression works as validation fine:
//tbody//td[text()='findMe']

Filtering Elements in XPath based on their descendants' text

Suppose I have a table with the following rows,
...
<tr>
<th title="Library of Quintessential Memes">LQM:</th>
</tr>
<tr>
<th title="Library of Boring Books">LBB:</th>
</tr>
...
I would like to select all <tr> elements whose first <th> child's text starts with "L". How can I do this using XPath selectors?
Use the starts-with function:
//tr[starts-with(th[1],"L")]

xpath expression to find url and data

i want to get the values of every table and the href value for every within the table given below.
Being new to xpath, i am finding it difficult to write xpath expression.
However understanding what an xpath expression does lies somewhat in an easier category.
the expected output
http://a.com/ data for a 526735 Z
http://b.com/ data for b 522273 Z
http://c.com/ data for c 513335 Z
<table class = dataTabe>
<tbody>
<tr>
<td>data for a</td>
<td class="numericalColumn">526735</td>
<td class="numericalColumn">Z</td></tr>
<tr>
<td>data for b</td>
<td class="numericalColumn">522273</td>
<td class="numericalColumn">B</td></tr>
<tr>
<td>data for c</td>
<td class="numericalColumn">513335</td>
<td class="numericalColumn">B</td></tr>
</tbody>
</table>
You'll need two things: an XPath query which locates the wanted nodes and a second which outputs the text as you want it. Since you don't give more information about the languages you're using I'm putting together some pseudocode:
foreach node in document.select("//table[class='dataTable']//tr[td/a/#HREF]")
write node.select("concat(td/a/#HREF,' ',.)")
This site has a great free tool for building XPath Expressions (XPath Builder):
http://www.bubasoft.net/
Use this XPath: //tr/td/a/#HREF | //tr//text()

Using tables with RedCloth inserts a lot of extra <br/> before each table

I am using the tables in RedCloth, for example
|cat|yeah|what|
|beery|true|fly|
|baru|false|mirror|
But I get a lot of <br/> and then the table....
What is going on and how can I fix this?
That table doesn't produce any <br/>s when formatted using the redcloth.org "try it" box on their homepage. However, something like this:
a
|cat|yeah|what|
|beery|true|fly|
|baru|false|mirror|
produces:
<p>a<br />
|cat|yeah|what|<br />
|beery|true|fly|<br />
|baru|false|mirror|</p>
So I would guess that you have a paragraph right before the tables without a blank link separating them. Adding an empty line:
a
|cat|yeah|what|
|beery|true|fly|
|baru|false|mirror|
Produces what is probably the desired result:
<p>a</p>
<table>
<tr>
<td>cat</td>
<td>yeah</td>
<td>what</td>
</tr>
<tr>
<td>beery</td>
<td>true</td>
<td>fly</td>
</tr>
<tr>
<td>baru</td>
<td>false</td>
<td>mirror</td>
</tr>
</table>

Resources