beginner
beginner

Reputation: 431

XPath expression to access each element of a string

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?

Upvotes: 1

Views: 264

Answers (2)

Michael Kay
Michael Kay

Reputation: 163595

You can also use

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

Upvotes: 4

wst
wst

Reputation: 11771

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)

Upvotes: 2

Related Questions