简体   繁体   中英

Query complex XML in SQL Server 2008 R2

I am trying to parse through an XML in SQL Server 2008 R2 and I am having some issues. I am trying to parse through the items for each parent node, but I can't seem to get the data accurate. The XML data is below:

 <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Project.xsd" Name="Job">
  <Design>
    <Catalog>
      <Identification>
        <Name>Group1</Name>
      </Identification>
      <FeatureSet ID="IDFS-1" Type="Style">
        <Description>Style1</Description>
      </FeatureSet>
      <Item ID="IDI-1-2730">
        <UserCode>Item1</UserCode>
        <LineItemNumber>1</LineItemNumber>
      </Item>
      <Item ID="IDI-1-1595">
        <UserCode>Item2</UserCode>
        <LineItemNumber>2</LineItemNumber>
      </Item>
    </Catalog>
    <Catalog>
      <Identification>
          <Name>Group2</Name>
      </Identification>
      <FeatureSet ID="IDFS-1" Type="Style">
        <Description>Style2</Description>
      </FeatureSet>
      <Item ID="IDI-1-2730">
        <UserCode>Item3</UserCode>
        <LineItemNumber>1</LineItemNumber>
      </Item>
    </Catalog>
  </Design>
</Project>

This is the SQL that I have so far, but doesn't seem to be working.

select
    x.d.query('./UserCode').value('.','char(40)') as UserCode
    ,x.d.query('./LineItemNumber').value('.','char(40)') as lineitemnumber
    ,i.d.query('./Name').value('.','nvarchar(200)') as [Group]
    ,u.d.query('./Description').value('.','nvarchar(200)') as Style
from 
    @xml.nodes('/Project/Design/Catalog/Item') as x(d)
outer apply 
    @xml.nodes('/Project/Design/Catalog/Identification') as i(d)
outer apply 
    @xml.nodes('/Project/Design/Catalog/FeatureSet') as u(d)

Obviously I am setting @xml to the xml data above with the XML data type.

Below are the results that I am getting:

UserCode    lineitemnumber  Group   Style
Item1       1               Group1  Style1
Item1       1               Group1  Style2
Item1       1               Group2  Style1
Item1       1               Group2  Style2
Item2       2               Group1  Style1
Item2       2               Group1  Style2
Item2       2               Group2  Style1
Item2       2               Group2  Style2
Item3       1               Group1  Style1
Item3       1               Group1  Style2
Item3       1               Group2  Style1
Item3       1               Group2  Style2

What I am looking for is:

UserCode    lineitemnumber  Group   Style
Item1       1               Group1  Style1
Item2       2               Group1  Style1
Item3       1               Group2  Style2

Every catalog will only have one Identification/Description and will also only have one Featureset/Description

select
     x.d.query('./UserCode').value('.','char(40)') as UserCode
    ,x.d.query('./LineItemNumber').value('.','char(40)') as lineitemnumber
    ,x.d.query('../Identification/Name').value('.','nvarchar(200)') as [Group]
    ,x.d.query('../FeatureSet/Description').value('.','nvarchar(200)') as Style
from @xml.nodes('/Project/Design/Catalog/Item') as x(d)

As mentioned by @Backs, parent node queries can perform badly, so you can avoid it by CROSS APPLYing from the parent to the children, then you can refer to either part as needed:

select
    -- these 2 are from the child (Item)
    c.i.value('(UserCode/text())[1]', 'char(40)') as UserCode,
    c.i.value('(LineItemNumber/text())[1]', 'char(40)') as lineitemnumber,

    -- these are from the parent (Catalog)
    d.c.value('(Identification/Name/text())[1]', 'nvarchar(200)') as [Group],
    d.c.value('(FeatureSet/Description/text())[1]', 'nvarchar(200)') as Style
from
    @xml.nodes('/Project/Design/Catalog') d(c) -- the parent node
    cross apply d.c.nodes('Item') c(i) -- the Item nodes under the Catalog parent

This has a much better estimated plan in comparison

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM