Friday, March 9, 2012

Getting nodes and node information from XML using XQuery

Hi,
I haev the following code:
DECLARE @.x xml
SET @.x='
<Root>
<row id="1"><name>Larry</name><oflw>some text</oflw></row>
<row id="2"><name>Joe</name></row>
<row id="3" />
</Root>
'
exec sp_xml_preparedocument @.idoc OUTPUT, @.x
SELECT * FROM OPENXML(@.idoc, '/Root')
This gives the following details
id
parentid
nodetype localname
prefix
namespaceuri
datatype
prev text
I want to get the same details using XQuery, please let me know how to
go about it.
Regards,
ShilpaYou cannot get the nodeids directly using XQuery or the used prefixes. You
can get the following though:
select n.value('if (. instance of element()) then 1
else if (. instance of attribute()) then 2
else if (. instance of text()) then 3
else 99
(:also add for PIs and comments:)', 'int') as nodetype,
n.value('local-name(.)', 'nvarchar(1000)') as localname,
n.value('namespace-uri(.)', 'nvarchar(1000)') as namespaceuri -- add more
logic for getting NULL if no namespace given instead of zero-length string
from @.x.nodes('//*,//@.*') as N(n)
HTH.
Michael
"Shilpa" <shilpa.nagavara@.unisys.com> wrote in message
news:1144319921.912049.213810@.j33g2000cwa.googlegroups.com...
> Hi,
> I haev the following code:
> DECLARE @.x xml
> SET @.x='
> <Root>
> <row id="1"><name>Larry</name><oflw>some text</oflw></row>
> <row id="2"><name>Joe</name></row>
> <row id="3" />
> </Root>
> '
> exec sp_xml_preparedocument @.idoc OUTPUT, @.x
> SELECT * FROM OPENXML(@.idoc, '/Root')
> This gives the following details
> id
> parentid
> nodetype localname
> prefix
> namespaceuri
> datatype
> prev text
>
> I want to get the same details using XQuery, please let me know how to
> go about it.
> Regards,
> Shilpa
>

No comments:

Post a Comment