XPath expression to access each element of a string

258 Views Asked by At

This question may sound very basic; however, I have been trying to find some resources related to it.

Let's say there is a sequence of characters or strings:

let $abc := ('a', 'b', 'c', 'd', 'e', 'f')
return (for $i in $abc return $i)

Above query will give me individual elements of a sequence.

However, if I have only a single string, how to retrieve each element of it?

e.g. let $abc = "abc"

How to fetch 'a' from $abc, is there any $abc[0] as such to do so?

2

There are 2 best solutions below

0
Michael Kay On BEST ANSWER

You can also use

for $char in string-to-codepoints($abc)!codepoints-to-string(.)
return ...
0
wst On

You can't access a string as an array in XQuery, but you can use substring:

let $abc := "abcd"
for $i in (1 to string-length($abc))
return substring($string, $i, 1)