Solved

XML Nodes and value

Posted on 2013-06-14
3
477 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 60

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CASE Statement for unknown # of columns 10 67
ms sql and asp dates 5 42
SQL - Creating Pivot with all the possible columns 7 33
store vs query adhoc - no show rows 4 37
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

739 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