Solved

XML Nodes and value

Posted on 2013-06-14
3
481 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

623 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