Reputation: 4346
I am trying to select the second node()
but only if it is a text node.
<p><span>Span text</span> plain text</p>
I have tried /p/node()[2]/text()
but that doesn't work because there isn't a text node inside the second node. /p/node()[1]/text()
gives 'Span text'
and /p/text()[1]
gives ' plain text'
but it should be using [2]
somewhere instead of [1]
. I can't work out how you specify a text predicate after you've selected the node.
The result I want is something like:
NodeList {
length => 2,
0 => Node('<span>Span text</span>'),
1 => Text(' plain text')
}
where the first node must be a span
and the second node must be text only. Preferably, there shouldn't be any other nodes so if there were more than two it shouldn't match anything and if the text node is missing it shouldn't match the span
node.
/p/node()[1]|/p/node()[2]
returns what I want when the input is good but it also returns matches outside of my requirements. e.g:
<p>Bad match <span>Span text</span> plain text</p>
would return 'Bad match '
as node()[1]
. This example of an invalid match should return the following:
NodeList {
length => 0
}
Upvotes: 0
Views: 381
Reputation: 89325
How about something like this :
/p[node()[2] = text()]
That will select <p>
with the 2nd child node equal to a text child node. Then you can continue the path by selecting any desired child of the <p>
, for example :
/p[node()[2] = text()]/node()[1]|/p[node()[2] = text()]/node()[2]
Upvotes: 1