Skip to content

shannonlowder.com

Menu
  • About
  • Biml Interrogator Demo
  • Latest Posts
Menu

SQL 301–FOR XML PATH

Posted on February 21, 2011 by slowder

OK, I’ve covered FOR XML RAW for simple quick XML outputs.  Then I covered FOR XML AUTO, while it automatically handles simple hierarchical data, it’s still pretty limited.  And last time I covered FOR XML EXPLICIT, which is the end all – be all when it comes to building XML exactly the way you want to.  But Microsoft felt there needed to be an easier way to construct XML.

 

Enter the Dragon PATH.  The idea was you could build your XML structure using an XPath-like method.  Let’s look at a an example of a FOR XML PATH query.

   1: SELECT

   2:     c.ContactID AS "@ID" 

   3:     ,c.FirstName + ' ' + c.LastName AS "@FullName" 

   4:     ,c.rowguid AS "node()"

   5:     ,CAST('<XMLExample/>' AS XML) AS "node()" 

   6:     ,c.RowGuid AS "comment()" 

   7:     ,e.SalariedFlag AS "JobInfo/@Salaried" 

   8:     ,e.Title AS "JobInfo/text()" 

   9: FROM Person.Contact AS c 

  10: INNER JOIN HumanResources.Employee e

  11:     on c.ContactID = e.ContactID

  12: WHERE 

  13:     c.LastName = 'Skywalker'

  14: FOR XML PATH('Employee'), ROOT('Employees');

Ok, I’ll be honest, I’m not an authority on XPath.  XPuath is considered part of the .NET Framework… that puts it a bit outside my specialty.  But let’s go through the code and I can explain it to you.

Unlike XML EXPLICIT, the declaration for the first two nodes, Employees and Employee, are built at the bottom, see line 14.  This makes it more like XML RAW or AUTO than EXPLICIT.  You declare the PATH your data will be in as well as you wrap your results in a ROOT node on line 14.

With those two nodes set up, we move on to setting up attributes of the first node.

We’re going to set up two attributes for every Employee, ID and Full Name.  You can see I pull ContactID from Person.Contact to populate ID. Then I concatenate FirstName and LastName from Person.Contact to fill in the FullName Attribute.

Next I wanted to show you the node() function, you can pass text, or xml to a node() alias, if you just pass text (or something that converts to text ie an INT would work, and be added as text to the XML)  It will simply add the text in for that node.  That’s what I’m doing on line 4.

However, you can pass fully formed XML to node() alias and it would add that XML fragment to your xml output.  That’s what I’m doing on line 5.

Next I wanted to show you how to add a comment, you select some data and alias it as “comment()”.  The interpreter will then add comments to your XML output.

On lines 7 and 8 I show you how to create data in child nodes.  In this case the child data is information from HumanResources.Employee.  I called it JobInfo.  I wanted to include whether or not the employee was salaried in an attribute called Salaried, and then I wanted to add the title the employee had as text using the alias text().

You can interchange all these aliases as you wish.  It’s supposed to make building XML easier. 

Let’s look at the results.

   1: <Employees>

   2:   <Employee ID="25" FullName="Luke Skywalker">

   3:     4865D7B7-5961-43CA-AD25-9F77D9D5E4A8

   4:     <XMLExample />

   5:     <!--4865D7B7-5961-43CA-AD25-9F77D9D5E4A8-->

   6:     <JobInfo Salaried="0">Marketing Assistant</JobInfo>

   7:   </Employee>

   8: </Employees>

Hrm, I could have swore we hired on Luke as a Jedi Master, tasked with teaching the next generation of padawans in the ways of The Force.  I’ll have to talk this over with HR.

FOR XML PATH is nice and all, but I just feel more comfortable with XML EXPLICIT…for now.  The up side to this method is you can add comments and other types of XML constructs to the output.  If you’re asked about adding constructs other than elements, nodes, or attributes, they’re probably asking you to use FOR XML PATH.

If you have any questions for me, please send them in.  I’m here to help.  Luke is here to rescue you…not assist with marketing.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • A New File Interrogator
  • Using Generative AI in Data Engineering
  • Getting started with Microsoft Fabric
  • Docker-based Spark
  • Network Infrastructure Updates

Recent Comments

  1. slowder on Data Engineering for Databricks
  2. Alex Ott on Data Engineering for Databricks

Archives

  • July 2023
  • June 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • October 2018
  • August 2018
  • May 2018
  • February 2018
  • January 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • March 2017
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • February 2013
  • January 2013
  • August 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • November 2004
  • September 2004
  • August 2004
  • July 2004
  • April 2004
  • March 2004
  • June 2002

Categories

  • Career Development
  • Data Engineering
  • Data Science
  • Infrastructure
  • Microsoft SQL
  • Modern Data Estate
  • Personal
  • Random Technology
  • uncategorized
© 2025 shannonlowder.com | Powered by Minimalist Blog WordPress Theme