Solved

XML Nodes and value

Posted on 2013-06-14
3
473 Views
Last Modified: 2013-06-15
Is there anway of getting the value if it exists on my XML Nodes output below?

SELECT x.n.value('fn:local-name(.)', 'varchar(255)') AS [Node]
     , x.n.value('fn:local-name(..)', 'varchar(255)') AS [ParentNode]
FROM @XmlDoc.nodes('//*') x(n)

Open in new window


So that if I have a node
<id>12345</id>

I could add a column for that in my output below
xx
0
Comment
Question by:lrbrister
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 39249128
Yes, you would just do something like:
SELECT x.n.value('fn:local-name(.)', 'varchar(255)') AS [Node]
     , x.n.value('fn:local-name(..)', 'varchar(255)') AS [ParentNode]
    , x.n.value('./text()[1]', 'varchar(max)') AS [NodeText]
FROM @XmlDoc.nodes('//*') x(n)
;

You can adjust data type as necessary with other XML documents, and scenarios. Another way to do this is with query() where you do not have to specify the type. It returns the node/element as XML.

SELECT x.n.query('fn:local-name(.)') AS [Node]
     , x.n.query('fn:local-name(..)') AS [ParentNode]
     , x.n.query('./text()[1]') AS [NodeText]
FROM @XmlDoc.nodes('//*') x(n)
;

I hope that helps!

P.S. The /text()[1] limits the result to the text part of the node, which alleviates your getting text for the node's children as well. For example, with just '.', the org.m5.api.v1.Response node would show text '07726' versus NULL.
0
 

Author Comment

by:lrbrister
ID: 39249194
I will test in the mirning
0
 

Author Closing Comment

by:lrbrister
ID: 39250032
Perfect
Thanks
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question