dbTalk Databases Forums  

Recursively Count Nodes

microsoft.public.sqlserver.xml microsoft.public.sqlserver.xml


Discuss Recursively Count Nodes in the microsoft.public.sqlserver.xml forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Derek Hart
 
Posts: n/a

Default Recursively Count Nodes - 07-19-2010 , 12:04 PM






Using SQL Server 2008
XML Data Type For A Column

How can I recursively count all the nodes within a node...

I have a node that is simply //MyNode[@Item="1"]

There are many nodes beneath this one, and nodes within nodes, hence the
recursive part. I want to count them.

I would also like to get a list of the nodes, such as a list of all the
values of @Item.

Help!

Reply With Quote
  #2  
Old   
Martin Honnen
 
Posts: n/a

Default Re: Recursively Count Nodes - 07-19-2010 , 12:22 PM






Derek Hart wrote:
Quote:
Using SQL Server 2008
XML Data Type For A Column

How can I recursively count all the nodes within a node...

I have a node that is simply //MyNode[@Item="1"]

There are many nodes beneath this one, and nodes within nodes, hence the
recursive part. I want to count them.
Well with XPath/XQuery you could do e.g.
count(//MyNode[@Item="1"]//*)
to count all element nodes contained in that MyNode.
But: MS SQL server supports only a very restricted form of XQuery so you
will have to try yourself whether it accepts that expression or complains.

Quote:
I would also like to get a list of the nodes, such as a list of all the
values of @Item.
//MyNode[@Item="1"]//@Item
gives you the Item attributes of everything contained in MyNode. But
again I am not sure SQL server will support that expression.


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #3  
Old   
Derek Hart
 
Posts: n/a

Default Re: Recursively Count Nodes - 07-19-2010 , 12:31 PM



The Count worked great!

This is the line I am using:
select FileTreePrivatexml.query('//Node//@GUID') from tuser where UserID=186

It gives the error XQuery [tuser.FileTreePrivateXML.query()]: Attribute may
not appear outside of an element

Do you think I am using it how you suggested?

Ultimately, I need to get a list of the @GUID values, and compare that
against a relational table with a join. I was thinking if I could somehow
insert these into a temp table that might to the trick.

Please let me know if you have any thoughts on getting the list in a stored
proc, and if my idea is solid about using a temp table. Might ask for help
on that if I get further here...


"Martin Honnen" <mahotrash (AT) yahoo (DOT) de> wrote

Quote:
Derek Hart wrote:
Using SQL Server 2008
XML Data Type For A Column

How can I recursively count all the nodes within a node...

I have a node that is simply //MyNode[@Item="1"]

There are many nodes beneath this one, and nodes within nodes, hence the
recursive part. I want to count them.

Well with XPath/XQuery you could do e.g.
count(//MyNode[@Item="1"]//*)
to count all element nodes contained in that MyNode.
But: MS SQL server supports only a very restricted form of XQuery so you
will have to try yourself whether it accepts that expression or complains.

I would also like to get a list of the nodes, such as a list of all the
values of @Item.

//MyNode[@Item="1"]//@Item
gives you the Item attributes of everything contained in MyNode. But again
I am not sure SQL server will support that expression.


--

Martin Honnen --- MVP Data Platform Development
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.