简体   繁体   中英

Using an absolute path after XPath following-sibling

I am using the following XPath search:

getNodeSet(doc, "//img[@src = './images/min_es.gif']/../../following-sibling::tr")

The result is a <tr> that contains two table (one within the other) so:

<tr valign="top"><td height="163">&#13;
    <table width="128" border="0" cellspacing="0" cellpadding="0" height="163"><tr valign="top"><td width="96">&#13;
          <table width="126"><tr><td width="65%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   Microclina</font></td>&#13;
                                 <td width="35%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   ~40 %</font></td>&#13;
                               </tr><tr><td width="65%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   Quartzo</font></td>&#13;
                                 <td width="35%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   ~29 %</font></td>&#13;
                               </tr><tr><td width="65%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   Plagioclase</font></td>&#13;
                                 <td width="35%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   ~20 %</font></td>&#13;
                               </tr><tr><td width="65%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   Biotite</font></td>&#13;
                                 <td width="35%" valign="top"><font size="1" face="Arial, Helvetica, sans-serif">&#13;
                                   ~10 %</font></td>&#13;
                               </tr></table></td>&#13;
      </tr></table></td>&#13;
</tr> 

Now I would like to access the inner table. To do this I have tried:

getNodeSet(doc, "//img[@src = './images/min_es.gif']/../../following-sibling::tr//table")

which give me a list of all of the tables (in this case the two tables above, one of which has both tables).

I have also tried:

 getNodeSet(doc, "//img[@src = './images/min_es.gif']/../../following-sibling::tr//table/table")

which gets me nothing. I have tried many variation among them this one:

getNodeSet(doc, "//img[@src = './images/min_es.gif']/../../following-sibling::tr[1]/.//table/table")

but I cannot get to that elusive inner table.

Can anyone show me how? Any references appreciated.

The problem here is that you are throwing in // haphazardly when you don't seem to understand what it does.

path//table

Means "select all tables anywhere beneath path . That's why it was selecting all the tables when you tried it. What you should be using is either:

//img[@src = './images/min_es.gif']/../../following-sibling::tr/table/tr/td/table

or

//img[@src = './images/min_es.gif']/../../following-sibling::tr/table//table

The first one means:

//img[@src = './images/min_es.gif']/../../following-sibling::tr

Select the tr

/table

Select any table s that are children of that tr .

/tr

Select any tr s that are children of those table s.

/td

Select any td that are children of those tr s.

/table

Select any table s that are children of those td s.

In the second example, we have:

/table

Select any table s are children of the selected the tr (s).

//table

Select any tables that are anywhere below that table .

I have solved this. Seems like I don't quite understand XPath yet. Solution is

`getNodeSet(doc, "//img[@src = './images/min_es.gif']/../../following-sibling::tr//table//table")`

Whenever a query returns one or more elements, you have to use "//" to match on the answer set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM