XQuery to parse a space-delimited string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phantom

    XQuery to parse a space-delimited string

    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

    #2
    Re: XQuery to parse a space-delimited string

    Phantom a écrit :
    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 :


    Read this section in paticular :


    Cheers,

    p.

    Comment

    • Martin Honnen

      #3
      Re: XQuery to parse a space-delimited string

      Phantom wrote:
      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

      Comment

      • Joe Kesselman

        #4
        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

        Comment

        • Phantom

          #5
          Re: XQuery to parse a space-delimited string

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

          Comment

          Working...