Moak
Moak

Reputation: 12885

Split comma separated string in xquery?

I have a div element I want to split in xquery
My start

<tags>{for $tag in //div[@id='tags']
return 
   <tag>{$tag}</tag>}</tags>

The target is converting this

<div id="tags">Tagged: tag1, TAG2, tag3</div>

The expected result is

<tags><tag>tag1</tag><tag>TAG2</tag><tag>tag3</tag></tags>

Upvotes: 2

Views: 5550

Answers (1)

kelloti
kelloti

Reputation: 8951

Supposedly you can use the tokenize(string, pattern) function to split strings based on a pattern. SQL Server 2008 doesn't have the function, but this could be your answer If whatever you're using does. I imagine it would look like this if I could get it to work:

 <tags>{
   for $tag in tokenize(
                 substring-after(
                   (//div[@id='tags'])[1],
                   'Tagged:'
                 ),
                 ','
               )
   return 
     <tag>{
       normalize-space($tag)
    }</tag>
}</tags>

Upvotes: 6

Related Questions