Get the non-empty element using XPATH
48,326
Solution 1
How about something like /root/group/p1[text() and not(../following-sibling::group/p1/text())]
In other words: get the p1 elements that have text and whose group parents are not followed by group nodes that have non-empty p1 elements.
Solution 2
This xpath should work as well:
//group/p1[string-length(text()) > 0]
Solution 3
You may also use [not(node())]
Selector.
Example: //group/p1[not(node())]
Related videos on Youtube
![Rowel](https://i.stack.imgur.com/P3NgK.jpg?s=256&g=1)
Author by
Rowel
Updated on July 02, 2020Comments
-
Rowel about 4 years
I have the following XML
<?xml version = "1.0" encoding = "UTF-8"?> <root> <group> <p1></p1> </group> <group> <p1>value1</p1> </group> <group> <p1></p1> </group> </root>
is it possible to get the last the node with value? in this case get the value of the second group/p1.
-
MikroDel about 11 yearsPost your your xslt or xpath please
-
-
Marcus about 11 yearsI did, it returns one node as intended.
-
Rowel about 11 yearsthis one works, i tried it here and it gives me what i want xpathtester.com/test
-
ovejaexiste almost 10 yearsIt also works with attributes like src //group/p1[string-length(@src) > 0]. Thanks!
-
chainstair over 4 yearsFor me this worked, because I had texts with only one backspace inside: [string-length(text()) > 1] Thank you a lot @Nora :)
-
blisstdev about 4 yearsShouldn't this be the inverse of that?
//group/p1[node()]
-
Digital Farmer almost 3 yearsAmazing tip @Nora