XQuery to parse a space-delimited string

P

Phantom

I'm wondering if anyone knows a trick to iterate a FLWR expression
across a literal string that is delimited by spaces.

e.g.

for $x in ("apple orange banana blueberry")

obviously, this doesn't work, since the string is delimited by a space
rather than being well-formed XML.

how would one get around this? I have no control over the incoming XML,
so I can't enforce proper encoding... I have to work across the string.

it did occur to me just to slice apart the string and do a for $index
in (1 to $wordCount) , but I couldn't figure out how to count words
within Xpath.

thanks in advance.
p
 
P

Phantom

for $x in tokenize("apple orange banana blueberry", " ")

See http://www.w3.org/TR/xpath-functions/#func-tokenize

Beautiful, Martin (and Peyo), tokenize was the spell I needed:

for $id in tokenize(string($r), " ")
return <id>{string($id)}</id>


Very slick, thank you! I'll remeber you every time I tokenize 8^)

Joe, I'd considered the recursion as a last ditch effort; it always
gives me headaches, and I was afraid of the slow down for large strings.

Have a great halloween, guys!

p.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,079
Latest member
ElidaWarin

Latest Threads

Top