Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

My technology blog on SQL Server, TSQL, XML, FILESTREAM and other areas of SQL Server.
Browse by Tags · View All
XML 112
TSQL 69
XQuery 69
XQuery Functions 67
XQuery Training 65
XQuery in TSQL 64
XQuery Tutorial 63
SQL Server XQuery 63
XQuery-Labs 57
BRH 38

Archive · View All
September 2008 32
August 2008 30
July 2008 21
August 2009 19
June 2009 19
May 2010 18
January 2009 15
January 2010 14
October 2008 14
June 2008 13

XQuery Lab 23 - Retrieving values and position of elements

Aug 21 2008 1:13AM by Jacob Sebastian   

 

I wrote this query a few months back to help some one in the XML forums. Here is the sample XML data for this lab.

<Names>
<Name>Jacob</Name>
<Name>Steve</Name>
<Name>Bob</Name>
</Names>

The task is to read the value from the <name> elements. Well that is pretty simple. The tough part is that, we need to retrieve the position of each element too. We need to retrieve 1 for Jacob, 2 for Steve and 3 for Bob.

XQuery does not provide a function to retrieve this information. The "position()" function cannot be used in the value() method. Hence I wrote the following query, which is little funny. It joins the XML nodes with a system table spt_values which contains a sequence of numbers. The number column is joined with the position() of each element and that gives us the position of each element. Here is the query:

DECLARE @x XML 
SELECT @x = '
<Names>
<Name>Jacob</Name>
<Name>Steve</Name>
<Name>Bob</Name>
</Names>'


SELECT
p.number as Position,
x.value('.','VARCHAR(10)') AS Name
FROM
master..spt_values p
cross APPLY @x.nodes('/Names/Name[position()=sql:column("number")]') n(x)
where p.type = 'p'

/*
Position Name
----------- ----------
1 Jacob
2 Steve
3 Bob
*/

XQuery Labs - A Collection of XQuery Sample Scripts and Tutorials

Tags: XQuery-Labs, XML, XQuery, XQuery Functions, SQL Server XQuery, XQuery in TSQL, XQuery Training, XQuery Tutorial,


Jacob Sebastian
1 · 100% · 32004
2
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

4  Comments  

  • Very interesting Post. I was looking for some thing like this for a very long time.

    I am not sure if you have covered this already, but the above approach is very useful in handling comma seperated strings and implementing array like functions.

    For Ex:

    DECLARE @Value NVARCHAR(MAX) SET @Value = '1,a,3,s,5,c,z,f'

    SET @x = '' + '' + REPLACE( @Value, ',', '') + '' +

    Now, you have the XML used in your post.

    BTW, can you cover few more functions like position(), if there are.

    commented on Jan 25 2010 12:41PM
    anand348
    1094 · 0% · 23
  • @anand, I recently added many more articles to the 'XQuery Labs' series. You may be able to find more functions there: http://beyondrelational.com/blogs/jacob/archive/2008/06/26/xquery-labs-a-collection-of-xquery-sample-scripts.aspx

    commented on Oct 16 2010 3:46PM
    JacobSebastian
    47 · 4% · 1215
  • I love this solution. Very creative. I'm trying to see how it would work to take it a step further. I have xml data in a column and in that xml there are elements I want to number, but also want to extract the child elements also numbered.

    So if the XML in the table (we'll call it scheduleInfo) is in a field called scheduleXML, is there a way to make it work.

    So one sample record in the xml would look like:

    <Schedules>
      <Name value="Bob">
        <Class>Math</Class>
        <Class>English</Class>
        <Class>Science</Class>
      </Name>
      <Name value="Joe">
        <Class>History</Class>
        <Class>Math</Class>
        <Class>English</Class>
      </Name>
    </Schedules>

    What I'd like to get in my output is (position of the name, name, position of the class, class):

    1,Bob,1,Math
    1,Bob,2,English
    1,Bob,3,Science
    2,Joe,1,History
    2,Joe,2,Math
    2,Joe,3,English

    Any thoughts on how to do this? Seems I may have to break out the xml into another table to get the position, name and then the xml of the classes, and then possibly go from there? Very new to XML in SQL Server so trying to wrap my head around this. I couldn't even get this solution properly written to pull from a table instead of a variable (may have just missed something) so going further down is really twisting my brain.

    Thanks

    commented on Aug 29 2012 12:50AM
    madwizard78
    2903 · 0% · 2
  • Is this what you are looking for?

    DECLARE @x XML 
    SELECT @x = '
    <Schedules>
      <Name value="Bob">
    	<Class>Math</Class>
    	<Class>English</Class>
    	<Class>Science</Class>
      </Name>
      <Name value="Joe">
    	<Class>History</Class>
    	<Class>Math</Class>
    	<Class>English</Class>
      </Name>
    </Schedules>'
    
    SELECT
    	p.number as Position,
    	x.value('@value[1]','VARCHAR(10)') AS Name,
    	p2.Number AS Sr,
    	c.value('.','VARCHAR(10)') AS Class
    FROM
    master..spt_values p
    CROSS JOIN master..spt_values p2
    CROSS APPLY @x.nodes('/Schedules/Name[position()=sql:column("p.number")]') n(x) 
    CROSS APPLY x.nodes('Class[position()=sql:column("p2.number")]') b(c)
    where p.type = 'p' AND p2.type = 'P'
    /*
    Position    Name       Sr          Class
    ----------- ---------- ----------- ----------
    1           Bob        1           Math
    1           Bob        2           English
    1           Bob        3           Science
    2           Joe        1           History
    2           Joe        2           Math
    2           Joe        3           English
    */
    
    commented on Aug 29 2012 4:39AM
    Jacob Sebastian
    1 · 100% · 32004

Your Comment


Sign Up or Login to post a comment.

"XQuery Lab 23 - Retrieving values and position of elements" rated 5 out of 5 by 2 readers
XQuery Lab 23 - Retrieving values and position of elements , 5.0 out of 5 based on 2 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]