dbTalk Databases Forums  

Accessing node values in the where clause

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


Discuss Accessing node values in the where clause in the microsoft.public.sqlserver.xml forum.



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

Default Accessing node values in the where clause - 11-27-2009 , 03:11 AM






Hi,
I have an XML column in a table and i need to fetch the record based on some
values, that are part of the XML as nodes.
The xml format looks like below.
<a>
<b id="">
<c>valuec</c>
</b>
</a>

I need to fetch the record based on the "<c>" values.
Can anyone tell me a way, that i can use the "<c>" value in the where clause?

When i tried using "CROSS APPLY" and ".nodes" , i was getting an error, node
not defined.

Iam doing all this in sql2008

Regards,
Pavan

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

Default Re: Accessing node values in the where clause - 11-27-2009 , 05:14 AM






Pavan wrote:
Quote:
Hi,
I have an XML column in a table and i need to fetch the record based on some
values, that are part of the XML as nodes.
The xml format looks like below.
a
b id=""
c>valuec</c
/b
/a

I need to fetch the record based on the "<c>" values.
Can anyone tell me a way, that i can use the "<c>" value in the where clause?

When i tried using "CROSS APPLY" and ".nodes" , i was getting an error, node
not defined.

Iam doing all this in sql2008
Which where clause exactly are you talking about, SQL, XQuery?
And you might not even need any of that as XQuery simply allows e.g.
/a/b[c = "valuec"]
where you put the condition into square brackets. The posted expression
selects the 'b' child elements 'of the 'a' root element where a 'c'
child of the 'b' has the string value "valuec".



--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #3  
Old   
Pavan
 
Posts: n/a

Default Re: Accessing node values in the where clause - 11-27-2009 , 06:13 AM



Hi Martin,
Actually i have multiple <c> nodes.
The code given by you works if i have only one <c> under <b>.

your help is appreciated.

"Martin Honnen" wrote:

Quote:
Pavan wrote:
Hi,
I have an XML column in a table and i need to fetch the record based on some
values, that are part of the XML as nodes.
The xml format looks like below.
a
b id=""
c>valuec</c
/b
/a

I need to fetch the record based on the "<c>" values.
Can anyone tell me a way, that i can use the "<c>" value in the where clause?

When i tried using "CROSS APPLY" and ".nodes" , i was getting an error, node
not defined.

Iam doing all this in sql2008

Which where clause exactly are you talking about, SQL, XQuery?
And you might not even need any of that as XQuery simply allows e.g.
/a/b[c = "valuec"]
where you put the condition into square brackets. The posted expression
selects the 'b' child elements 'of the 'a' root element where a 'c'
child of the 'b' has the string value "valuec".



--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
.

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

Default Re: Accessing node values in the where clause - 11-27-2009 , 06:32 AM



Pavan wrote:

Quote:
Actually i have multiple <c> nodes.
The code given by you works if i have only one <c> under <b>.
Please post a sample of the XML you have and explain which data you want
and which condition you want to test in plain English, then we can try
to express that with XQuery.
It is currently not clear to me what you want to achieve.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #5  
Old   
Pavan
 
Posts: n/a

Default Re: Accessing node values in the where clause - 11-30-2009 , 05:23 AM



<a>
<b id="">
<c>valuec</c>
<c>valuec1</c>
<c>valuec2</c>
</b>
</a>

I would like to have a query on "valuec1"..

something like
select ...
where c = 'valuec1'

Thanks,
Pavan

"Martin Honnen" wrote:

Quote:
Pavan wrote:

Actually i have multiple <c> nodes.
The code given by you works if i have only one <c> under <b>.

Please post a sample of the XML you have and explain which data you want
and which condition you want to test in plain English, then we can try
to express that with XQuery.
It is currently not clear to me what you want to achieve.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
.

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

Default Re: Accessing node values in the where clause - 11-30-2009 , 05:46 AM



Pavan wrote:
Quote:
a
b id=""
c>valuec</c
c>valuec1</c
c>valuec2</c
/b
/a

I would like to have a query on "valuec1"..

something like
select ...
where c = 'valuec1'
Well the path I suggested earlier e.g.

/a/b[c = "valuec1"]

does work, it does not matter how many 'c' child elements the 'b'
element has, the condition in the square brackets is true if there is at
least one 'c' child with string value 'valuec1'.


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

Reply With Quote
  #7  
Old   
Pavan
 
Posts: n/a

Default Re: Accessing node values in the where clause - 11-30-2009 , 06:32 AM



Thanks Martin....It worked...
The XML was not proper before..


"Martin Honnen" wrote:

Quote:
Pavan wrote:
a
b id=""
c>valuec</c
c>valuec1</c
c>valuec2</c
/b
/a

I would like to have a query on "valuec1"..

something like
select ...
where c = 'valuec1'

Well the path I suggested earlier e.g.

/a/b[c = "valuec1"]

does work, it does not matter how many 'c' child elements the 'b'
element has, the condition in the square brackets is true if there is at
least one 'c' child with string value 'valuec1'.


--

Martin Honnen --- MVP XML
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.