Tok Doom
Tok Doom

Reputation: 83

xpath, function identifies classes by searching elements?

I'm a very new beginner with xpath and html. Is it possible to search an html for a text "xxxx" under in each class "authorbox" and if the class has it, automatically select the parent class tr.

< tr>

< td class="authorbox">

 < div class="regsn">

      < a href="/member/profile/xxxx" t="post-usersntxt">xxxx< /a>

....

and the table contiues with more

< tr>

< tr>

< tr>

EDIT

This is the xpath I'm currently able to make

//td[@class='authorbox']

I don't really know how to search for text "xxxx" or select the parent tr if the text is found. I can select each table if I want to though, but it would be nice if it was more automatic.

Thanks

Upvotes: 1

Views: 74

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

Use:

//tr[td[@class='authorbox']
           //text()[contains(., 'xxxx')]
    ]

this selects any tr in the XML document that has a td child whose class attribute's string value is the string "authorbox" and that (the td child) has a text-node descendant whose string value is a string that contains "xxxx".

This may be made more precise:

If the text node descendant's string value must be exactly the string "xxxx", then use:

//tr[td[@class='authorbox']
           //text()[. = 'xxxx']
    ]

If the string value of the text-node descendant shoud start with the string "xxxx", use:

//tr[td[@class='authorbox']
           //text()[starts-with(., 'xxxx')]
    ]

If the string value of the text-node descendant shoud contains the string "xxxx" that is surrounded only by white-space, use:

//tr[td[@class='authorbox']
           //text()[normalize-space(., 'xxxx')]
    ]

Upvotes: 1

FatalError
FatalError

Reputation: 54551

So, it looks like you're halfway there. You just need to add a little beef to your predicate:

//tr[td/@class="authorbox" and td/div/a="xxxx"]

Also, if you want the tr, you can start with that as above, and push the td reference down into the predicate.

Upvotes: 1

Related Questions