XPath to find text node that is a sibling of other nodes
Given the following fragment of html:
<field开发者_JS百科set>
<legend>My Legend</legend>
<p>Some text</p>
Text to capture
</fieldset>
Is there an xpath expression that will return only the 'Text to capture' text node?
Trying
/fieldset/text()yields three nodes, not just the one I need.
Assuming what you want is the text node containing non whitespace text :
//fieldset/text()[normalize-space(.)]
If what you want is the last text node, then:
//fieldset/text()[last()]
I recommend you accept Steven D. Majewski's answer, but here is the explanation (text nodes highlighted with square brackets):
<fieldset>[
]<legend>My Legend</legend>[
]<p>Some text</p>[
Text to capture
]</fieldset>
so /fieldset/text()
returns
"\n "
"\n "
"\n Text to capture\n"
And this is why you want /fieldset/text()[normalize-space()]
, and you want the result trimmed before use.
Also note that the above is short for /fieldset/text()[normalize-space(.) != '']
. When normalize-space()
returns a non-empty string, the predicate evaluates to true
, while the empty string evaluates to false
.
精彩评论