CJ Dennis
CJ Dennis

Reputation: 4346

Using XPath how do I select a node() at a specific position that is also text()?

I want to select the previous node only if it is a text node (and contains only whitespace). I have an XPath like so: path/preceding-sibling::node()[1][normalize-space()='']. This works great but matches both text and element nodes (if the nodes contain only whitespace). Using path/preceding-sibling::text()[1][normalize-space()=''] will select the first preceding node that is a text node which is definitely not what I want if there are any elements in between.

How can I combine the two tests?

Upvotes: 1

Views: 196

Answers (1)

har07
har07

Reputation: 89315

You can use self::text() to test if current node is a text node, like so :

path/preceding-sibling::node()[1][self::text() and normalize-space()='']

Upvotes: 1

Related Questions