Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select XML nodes as rows

Tags:

sql

tsql

xml

xpath

I am selecting from a table that has an XML column using T-SQL. I would like to select a certain type of node and have a row created for each one.

For instance, suppose I am selecting from a people table. This table has an XML column for addresses. The XML is formated similar to the following:

<address>
  <street>Street 1</street>
  <city>City 1</city>
  <state>State 1</state>
  <zipcode>Zip Code 1</zipcode>
</address>
<address>
  <street>Street 2</street>
  <city>City 2</city>
  <state>State 2</state>
  <zipcode>Zip Code 2</zipcode>
</address>

How can I get results like this:

Name         City         State

Joe Baker   Seattle      WA

Joe Baker   Tacoma     WA

Fred Jones  Vancouver BC

like image 493
Bjørn Avatar asked Oct 10 '08 17:10

Bjørn


People also ask

How do I select a specific XML node in SQL Server?

You should use the query() Method if you want to get a part of your XML. If you want the value from a specific node you should use value() Method. Update: If you want to shred your XML to multiple rows you use nodes() Method.

What is XML node SQL Server?

The result of the nodes() method is a rowset that contains logical copies of the original XML instances. In these logical copies, the context node of every row instance is set to one of the nodes that is identified with the query expression. This way, later queries can navigate relative to these context nodes.


2 Answers

Here is your solution:

/* TEST TABLE */
DECLARE @PEOPLE AS TABLE ([Name] VARCHAR(20),  [Address] XML )
INSERT INTO @PEOPLE SELECT 
    'Joel', 
    '<address>
      <street>Street 1</street>
      <city>City 1</city>
      <state>State 1</state>
      <zipcode>Zip Code 1</zipcode>
    </address>
    <address>
      <street>Street 2</street>
      <city>City 2</city>
      <state>State 2</state>
      <zipcode>Zip Code 2</zipcode>
    </address>'
UNION ALL SELECT
    'Kim', 
    '<address>
      <street>Street 3</street>
      <city>City 3</city>
      <state>State 3</state>
      <zipcode>Zip Code 3</zipcode>
    </address>'

SELECT * FROM @PEOPLE

-- BUILD XML
DECLARE @x XML
SELECT @x = 
( SELECT 
      [Name]
    , [Address].query('
            for $a in //address
            return <address 
                street="{$a/street}" 
                city="{$a/city}" 
                state="{$a/state}" 
                zipcode="{$a/zipcode}" 
            />
        ') 
  FROM @PEOPLE AS people 
  FOR XML AUTO
) 

-- RESULTS
SELECT [Name]    = T.Item.value('../@Name', 'varchar(20)'),
       street    = T.Item.value('@street' , 'varchar(20)'),
       city      = T.Item.value('@city'   , 'varchar(20)'),
       state     = T.Item.value('@state'  , 'varchar(20)'),
       zipcode   = T.Item.value('@zipcode', 'varchar(20)')
FROM   @x.nodes('//people/address') AS T(Item)

/* OUTPUT*/

Name | street   | city   | state   | zipcode
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Joel | Street 1 | City 1 | State 1 | Zip Code 1
Joel | Street 2 | City 2 | State 2 | Zip Code 2
Kim  | Street 3 | City 3 | State 3 | Zip Code 3
like image 192
leoinfo Avatar answered Sep 25 '22 02:09

leoinfo


Here's how I do it generically:

I shred the source XML via a call such as



DECLARE @xmlEntityList xml
SET @xmlEntityList =
'
<ArbitrarilyNamedXmlListElement>
              <ArbitrarilyNamedXmlItemElement><SomeVeryImportantInteger>1</SomeVeryImportantInteger></ArbitrarilyNamedXmlItemElement>
              <ArbitrarilyNamedXmlItemElement><SomeVeryImportantInteger>2</SomeVeryImportantInteger></ArbitrarilyNamedXmlItemElement>
              <ArbitrarilyNamedXmlItemElement><SomeVeryImportantInteger>3</SomeVeryImportantInteger></ArbitrarilyNamedXmlItemElement>
</ArbitrarilyNamedXmlListElement>
'

    DECLARE @tblEntityList  TABLE(
        SomeVeryImportantInteger    int
    )

    INSERT @tblEntityList(SomeVeryImportantInteger)
    SELECT 
        XmlItem.query('//SomeVeryImportantInteger[1]').value('.','int') as SomeVeryImportantInteger
    FROM
        [dbo].[tvfShredGetOneColumnedTableOfXmlItems] (@xmlEntityList)



by utilizing the scalar-valued function


/* Example Inputs */
/*
DECLARE @xmlListFormat xml
SET     @xmlListFormat =
            '
            <ArbitrarilyNamedXmlListElement>
              <ArbitrarilyNamedXmlItemElement>004421UB7</ArbitrarilyNamedXmlItemElement>
              <ArbitrarilyNamedXmlItemElement>59020UH24</ArbitrarilyNamedXmlItemElement>
              <ArbitrarilyNamedXmlItemElement>542514NA8</ArbitrarilyNamedXmlItemElement>
            </ArbitrarilyNamedXmlListElement>
            '
declare @tblResults TABLE 
(
    XmlItem xml
)

*/

-- =============================================
-- Author:      6eorge Jetson
-- Create date: 01/02/3003
-- Description: Shreds a list of XML items conforming to
--              the expected generic @xmlListFormat
-- =============================================
CREATE FUNCTION [dbo].[tvfShredGetOneColumnedTableOfXmlItems]  
(
    -- Add the parameters for the function here
    @xmlListFormat xml
)
RETURNS 
@tblResults TABLE 
(
    -- Add the column definitions for the TABLE variable here
    XmlItem xml
)
AS
BEGIN

    -- Fill the table variable with the rows for your result set
    INSERT @tblResults
    SELECT
        tblShredded.colXmlItem.query('.')   as XmlItem
    FROM
         @xmlListFormat.nodes('/child::*/child::*') as tblShredded(colXmlItem)

    RETURN 
END

--SELECT * FROM @tblResults


like image 35
6eorge Jetson Avatar answered Sep 26 '22 02:09

6eorge Jetson