Filtering Elements in XPath based on their descendants' text - xpath

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")]

Related

How to get the count number under tbody, use xpath in UFT

I am using UFT to write the automation script, I want to to get the count number under one tbody element,(count how many 'tr', and return the result)
<tbody role="rowgroup">
<tr _calss="01">
<tr _calss="02">
<tr _calss="03">
<tr _calss="04">
<tr _calss="05">
</tbody>
How to write the code in UFT?
Since it is unclear if there can be more than one tbody you could use this for the first one.
count((//tbody)[1]/tr)

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']

Extract sub-elements from XPATH Fetch Page pipe

I'm hoping to convert the content of 3 adjacent TD elements in a TR using Yahoo Pipes to a comma-delimited list of values. Source: Epic Systems Hospitals.
HTML snippet:
...
<table width="623" cellspacing="0" cellpadding="0" border="0">
<colgroup>
<tbody>
<tr height="20">
<td width="425" height="20">Institution 0</td>
<td width="134">Minneapolis</td>
<td width="64">MN</td>
</tr>
<tr height="20">
<td height="20">Institution 1</td>
<td>Philadelphia</td>
<td>PA</td>
</tr>
...
I've used the "XPath fetch page" source to correctly isolate the TR elements using an XPATH=//tr[#height='20'].
I'm having difficulty getting the TD elements, however. It's not obvious to me which component I should be using, so I chose a Sub-element with the 'special variable substitution' syntax. Unfortunately, ${td.0.content} doesn't work.
What am I not understanding?
** edit **
My goal is to create an XML stream that resembles:
<institutions>
<institution name='Institution 0' city='Minneapolis' region='MN'/>
<institution name='Institution 1' city='Philadelphia' region='PA'/>
...
<institutions/>
If you always have 3 td cells, you could use a Loop operator with a String Builder inside, and build a string by concatenating item.td.0, item.td.1, item.td.2.
I created an example of this for you here:
http://pipes.yahoo.com/pipes/pipe.info?_id=3d24486f7c6e8413dc6252ef37c2f086

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()

Two levels of contains in an 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')]]

Resources