1

I have an HTML table:

<table>
  <tr>
    <td>
        <div class="header">test</div>
    </td>
    <td>
        <div class="header">
            <div class="subheader">
                <span>test2</span>
            </div>
        </div>
    </td>
  </tr>
</table>

I want to know how to select deepest element in <td> without needing to use multiple xpath queries.

//table/tr/td/*[last()]/text()

So the query result would be:

Text='test' Text='test2'

Rich
  • 6,470
  • 15
  • 32
  • 53
3lvinaz
  • 113
  • 3
  • 12
  • Hello Elvinai, see ancestor-or-self – toor Apr 08 '18 at 18:31
  • You can simply use `//table/tr/td//text()` there is no need in `last()` – Andersson Apr 08 '18 at 18:43
  • Thank you,but with this solution I need to know which index to select, for example, I get: 'test', '', '', test2, '','' – 3lvinaz Apr 08 '18 at 18:51
  • @3lvinaz , I see no `","` nodes on provided HTML sample – Andersson Apr 08 '18 at 18:55
  • For true deepest node selection in XPath, see [**this question**](https://stackoverflow.com/q/11135620/290085) – kjhughes Apr 08 '18 at 19:21
  • If the above link doesn't help, please [edit] your question and add more elements to your example that you wish ***not*** to be selected by your sought XPath. As it stands, your example fails to illustrate what you mean by "deepest". Thanks. – kjhughes Apr 08 '18 at 19:35
  • Sorry for that, I will try my best next time :). That question fits my requirements as well... I have so much to choose from now :D – 3lvinaz Apr 08 '18 at 19:48
  • If *deepest* and *last* both fit your requirements, it's likely that your requirements are ill-stated. *Deepest* pertains to a count along the ancestor-descendant direction; *last* refers to an ordering of nodes in a document sequence direction. – kjhughes Apr 08 '18 at 20:13

1 Answers1

1

Thanks to @Andersson I managed to select deepest element in <td>.

Query looks like this:

//table/tr/td/descendant::*[last()]

And query result is:

Element='<div class="header">test</div>' Element='<span>test2</span>'

Which node values are:

Element='test' Element='test2'

Everyone thank you so much for the effort and sorry for accepting answer which wasn't actually correct one.

3lvinaz
  • 113
  • 3
  • 12
  • 1
    Note that this still doesn't select the *deepest* descendant, but the last. For example on `
    test2
    test3` you'll get test3 rather than test2.
    – user3942918 Apr 08 '18 at 19:15
  • You're right in this point, but in my scenario this query fits perfectly, because we have only one deepest element, but maybe in the future there will be more than one. – 3lvinaz Apr 08 '18 at 19:36
  • Then perhaps you misunderstand what *deepest* means. @PaulCrovella has a very valid point. Note that your sample is too simple to differentiate *deepest* from *last*. – kjhughes Apr 08 '18 at 19:39