Selecting an element based on text and attribute of its sibling, using Xpath

Posted by Adam Asham on Stack Overflow See other posts from Stack Overflow or by Adam Asham
Published on 2010-03-21T18:10:00Z Indexed on 2010/03/21 18:21 UTC
Read the original article Hit count: 329

Filed under:
|
|

Looking at the document, the goal is to select the second cell from the second row, in the first table.

I've created the following expression:

//row/td[2]/text()[td[@class="identifier"]/span[text()="identifier"]]

but it does not return any rows. Unfortunately I do not see what's wrong.

To me, it looks alright. The expression should:

select the text
    in the second cell 
        in any row
where
    the text of a span equals to "identifier"
        and the span is located in cell with a "identifier" class

I'd appreciate it if you could point out what I'm doing wrong.

Sample XML document:

<?xml version="1.0"?>
<html>
    <table class="first">
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td class="identifier">
                <span>identifier</span>
            </td>
            <td>
                foo
                <span>ignore</span>
                bar
            </td>
        </tr>
        <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
        </tr>
    </table>

    <table class="second">
        <tr>
            <td>row 1, cell 1</td>
            <td>row 1, cell 2</td>
        </tr>
        <tr>
            <td class="identifier">
                <span>not an identifier</span>
            </td>
            <td>
                not a target
            </td>
        </tr>
        <tr>
            <td>row 3, cell 1</td>
            <td>row 3, cell 2</td>
        </tr>
    </table>
</html>

© Stack Overflow or respective owner

Related posts about c#

Related posts about xpath