tags:

views:

28

answers:

2

EDIT: Why was this voted down? I really have no idea... BTW ../ does not work since i don't want the parent of the Table but want actually ../td+1 i don't know if that is even possible?

Hi guys.

I have a fairly complex problem at hand..

I have a table like this:

Name | Result | AutotestID | AutotestResult | AutotestName

X | Pass | X86HZS1 | | |

So... What i want to do is the following. I know only the ID. And i'd like to update the AutotestResult from an autotest. Which is empty to begin with.

I'm trying to locate the ID... I have that. But then when i have the ID i must update the Row next to it. How do i do that? I tried playing with the xPath. Stepping backwards using ../../../td etcetc but with no luck.

I can't seem to find its neighboring table...

Could somebody please point me into the right direction?

Thanks very much for every help!

Hannibal

+1  A: 

Your question is unclear but I'm doing some guessing and presume you have an XHTML file

<table>
  <tr>
    <td>Name</td>
    <td>Result</td>
    <td>AutotestID</td>
    <td>AutotestResult</td>
    <td>AutotestName</td>
  </tr>
  <tr>
    <td>X</td>
    <td>Pass</td>
    <td>X86HZS1</td>
    <td></td>
    <td></td>
  </tr>
</table>

and you are trying to create an XPath expression that selects the next <td> element after the one that contains text "X86HZS1" (the AutotestID). To find a node with certain text content you can use a predicate and the sibling elements that appear after this node in the document order you get by using axis following-sibling. A proper XPath expression would be

//td[.='X86HZS1']/following-sibling::td[1]

If the same AutotestID you are matching appears several times in your table data, this expression matched all of them and would return a node set instead of a single node.

Edit: Generally using // when not neccessary is not recommended since it requires traversing through the whole document. Using a more direct path, like mentioned in comments, results in more efficient XPath expression.

jasso
@jasso: Good answer. But don't recommend starting `//` operator when it's not needed. You should use: `/table/td[.='X86HZS1']/following-sibling::td[1]`
Alejandro
Yep, I know `//` is slow and can often be avoided. This time I just used it because the whole structure of the XML doc was my guessing and I couldn't know what kind of tree OP was going to handle. You have a good point though, I probably still should have emphasised that it would be better to replace `//` with a more precise match.
jasso
+1  A: 

Besides jasso's good recommendation of following-sibling axis, there is some problem with that answer: you must fix what td is the AutotestID.

So, knowing the table' schema:

<table> 
  <tr> 
    <td>Name</td> 
    <td>Result</td> 
    <td>AutotestID</td> 
    <td>AutotestResult</td> 
    <td>AutotestName</td> 
  </tr> 
  <tr> 
    <td>X</td> 
    <td>Pass</td> 
    <td>X86HZS1</td> 
    <td></td> 
    <td></td> 
  </tr> 
</table> 

You could use:

/table/tr/td[3][.='X86HZS1']/following-sibling::td[1]

or

/table/tr[td[3]='X86HZS1']/td[4]

If you don't know the AutotestID position, you could use:

/table/tr/td[count(../../tr/td[.='AutotestID']/preceding-sibling::td)+1]
            [.='X86HZS1']/following-sibling::td[1]
Alejandro