mr. parchman
mr. parchman

Reputation: 93

Xpath: First letter/string immediately following element

I have the following XML possibilities:

<metamark/><anything>. <anything2>bla</anything2>.</anything>
<metamark/>.<anything> <anything2>bla</anything2>.</anything>
<metamark/><anything><anything2>bla</anything2>.</anything>

I want to get the first "." So I tried:

starts-with(metamark[not(descendant::text())]/following-sibling::text()[1],'.')

Works fine for line 2 above but I miss line 1. So i did:

starts-with(metamark[not(descendant::text())]/following-sibling::text()[1],'.') or starts-with(metamark[not(descendant::text())]/following-sibling::*[1]/text()[1],'.')

which now works for all lines above. However line 3 should not be true, because I only want "." that are either immediately following the empty <metamark/> or the first string of the immediately following sibling, BUT not if there is text in any descendant element of that sibling before the "."

So in plain words: the first text (as in nearest to the actual closing ">" after an element. How would I achieve that?

Upvotes: 2

Views: 1309

Answers (1)

Tomalak
Tomalak

Reputation: 338326

How about: "The first text node that follows any <metamark> as long as it starts with '.'"

//metamark[not(descendant::text())]/following::text()[1][starts-with(., '.')]

In

<r>
  <metamark/><anything>. yes1<anything2>bla</anything2>. no1</anything>
  <metamark/>. yes2<anything> <anything2>bla</anything2>. no2</anything>
  <metamark/><anything><anything2>bla</anything2>. no3</anything>
</r>

this retrieves

'. yes1'
'. yes2'

The following:: axis ignores nesting, which is useful for this task.

Upvotes: 2

Related Questions