1

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!

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
Magnus Vivadeepa
  • 57
  • 1
  • 2
  • 11
  • Did you do any research about this? Please, take a few moments to read [how to ask useful questions](https://stackoverflow.com/help/mcve). – LMC Feb 06 '18 at 13:03
  • sure i did research, but i can't find anything which just selects the first string. so if you have an idea, i would be really thankful – Magnus Vivadeepa Feb 06 '18 at 13:25
  • 1
    my XPath Query looks like this: for $a in //Ereignis/Abflug return ($a/Flugnr, $a/Route/text()) but IDK how i just get bangkok – Magnus Vivadeepa Feb 06 '18 at 13:27
  • Check this one https://stackoverflow.com/questions/4322013/getting-a-substring-of-an-attribute-in-xpath – LMC Feb 06 '18 at 13:52
  • yeah i saw it with substring-before. But the problem with this function is, that i don't know what is behind Bangkok or another city! The tokenize function works better! – Magnus Vivadeepa Feb 06 '18 at 14:01

2 Answers2

1

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])
har07
  • 88,338
  • 12
  • 84
  • 137
  • hey, thanks for that! I didn't recognize that tokenize is for that... So i tried it, and my programm tells me this error: "Expecting a Node but found an Atomic Value instead: 'Bangkok'" It has the right one, but doesn't return it? – Magnus Vivadeepa Feb 06 '18 at 13:53
  • It is likely related to the API of the XPath engine that you use. I don't know what your XPath engine is and how you use it, so the least I can suggest is to change the query to return uniform data type (probably change `$a/Flughr` to `$a/Flughr/string()`), if possible, and correct parameter that was explicitly mention about returning node, if any – har07 Feb 06 '18 at 14:05
0

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],' ')
zx485
  • 28,498
  • 28
  • 50
  • 59