开发者

using a node to select a grandparent to select one of it's descendents

开发者 https://www.devze.com 2023-02-24 09:53 出处:网络
I have a tree similar to this: tr _|_ tdtd _||_ divdiv _||_ input\"foobar\" I have the node \"foobar\' selected using:

I have a tree similar to this:

         tr
        _|_
       td  td
      _|    |_
     div     div
     _|       |_ 
 input       "foobar"

I have the node "foobar' selected using:

//*[开发者_高级运维normalize-space(text())='foobar']

But I am unable to select the great grandparent tr and select great grand child input with it. Any Suggestions?


I have the node "foobar' selected using:

//*[normalize-space(text())='foobar'] 

But I am unable to select the great grandparent tr and select great grand child "foobar" with it.

I guess that you want to select the element whose string value (after normalization) is "foobar", starting from the top element tr.

Here is one XPath expression that "starts" from the top elemet tr and selects the wanted element:

/tr//*[normalize-space()='foobar']

If you need to select the text node itself (not its element parent):

/tr//text()[normalize-space()='foobar']

If you want to avoid the costly and slow evaluation of the // abbreviation (because you know the document structure), use this expression:

/tr/td/div[normalize-space()='foobar']

or even:

/tr/td[2]/div

Update:

Now the OP changed his question and wants to select the input element from the div that has normalized string-value 'foobar'.

Here is an XPath expression selecting exactly this:

../..//input

or

../../td[1]/div/input

Do note: In case these expressions don't select any node, it is most likely that the document has a default namespace -- search for XPath default namespace -- there are many good answers to this problem -- here at SO and on the internet.


This XPath expression select every element having a descendant text node equal to "foobar" after whitespace normalization:

//*[.//text()[normalize-space()='foobar']]
0

精彩评论

暂无评论...
验证码 换一张
取 消