Subscribe To

Subscribe to SQL Server and XML by Email
Labels

Friday, July 11, 2008

XQuery Lab 7 - Extracting a comma separated list of values

This posts is moved to http://beyondrelational.com/blogs/jacob/archive/2008/07/11/xquery-lab-7-extracting-a-comma-separated-list-of-values.aspx

2 comments:

  1. What if the XML attribute value has spaces on its own?
    For example, Value="brown fox"

    The REPLACE() function technique won't work in such cases.

    Regards,
    Yitzhak

    ReplyDelete
  2. Yes, in such a case, you might need a little more complicated XQuery.

    DECLARE @x XML
    SELECT @x = '
    [Type Value="White Fox"/]
    [Type Value="Brown Fox"/]'

    SELECT SUBSTRING(@x.query('
    for $t in (/Type)
    return
    [Type]{concat('','',$t/@Value)}[/Type]
    ').query('data(Type)').value('.','varchar(50)'),2,50) AS Val

    /*
    Val
    --------------------------------------------------
    White Fox ,Brown Fox
    */

    Note: Blogger does not allow me to post XML fragments in comments and hence I replaced XML tags with []

    ReplyDelete

About Me
Jacob Sebastian
Tutorials
* XQuery Tutorials
My Links
SQL Server White Papers
SQL Server 2008
My Articles
XML Workshop RSS Feed
Contact me
Readers
Free Hit Counter
Web Site Hit Counters
SQL Server Bloggers
Blog Directories
blogarama - the blog directory Programming Blogs - BlogCatalog Blog Directory
 
Copyright Jacob Sebastian