Connecting Tech Pros Worldwide Forums | Help | Site Map

XQuery to parse a space-delimited string

Phantom
Guest
 
Posts: n/a
#1: Oct 17 '08
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


Peyo
Guest
 
Posts: n/a
#2: Oct 18 '08

re: XQuery to parse a space-delimited string


Phantom a écrit :
Quote:
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
There is deidcated XPath 2.0/XQuery function todo this :
http://www.w3.org/TR/xquery-operators/

Read this section in paticular :
http://www.w3.org/TR/xquery-operators/#string.match

Cheers,

p.
Martin Honnen
Guest
 
Posts: n/a
#3: Oct 18 '08

re: XQuery to parse a space-delimited string


Phantom wrote:
Quote:
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")
for $x in tokenize("apple orange banana blueberry", " ")

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

--

Martin Honnen
http://JavaScript.FAQTs.com/
Joe Kesselman
Guest
 
Posts: n/a
#4: Oct 18 '08

re: XQuery to parse a space-delimited string


Of course there are also recursive solutions (which is how XSLT 1.0
would have to solve that problem).

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
Phantom
Guest
 
Posts: n/a
#5: Oct 18 '08

re: XQuery to parse a space-delimited string


On 2008-10-18 04:11:28 -0700, Martin Honnen <mahotrash@yahoo.desaid:
Quote:
Phantom wrote:
Quote:
>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")
>
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.

Closed Thread