Magnus Vivadeepa
Magnus Vivadeepa

Reputation: 57

XPath 2.0 return first string in an element node

ich want to return just Bangkok and nothing more, how can i select just the first string? There has to be a function or something like that?

the element node looks like this:

<Route>Bangkok 05.00, Puket 06.30
      <Ziel>Melbourne an 21.30</Ziel> 
</Route> 

Thanks in advance!

Upvotes: 1

Views: 654

Answers (2)

zx485
zx485

Reputation: 29042

Selecting the first text() node can be achieved with

Route/text()[1]

And to only select 'Bangkok' you can use the substring-before XPath 1.0 function like this

substring-before(Route/text()[1],' ')

Upvotes: 0

har07
har07

Reputation: 89315

You can use tokenize() in XPath 2.0 to split text by space and return only the first tokenize result:

//Route/tokenize(., ' ')[1]

Or if the target text is always in the first text node child of Route then the following should be slightly more efficient :

//Route/tokenize(text(), ' ')[1]

demo

Adapted based on your attempted query :

for $a in //Ereignis/Abflug 
return ($a/Flugnr, $a/Route/tokenize(text(), ' ')[1])

Upvotes: 1

Related Questions