XML Shredding into SQL Server

天涯浪子 提交于 2019-12-24 07:27:54

问题


I have an XML doc in the following format.

<?xml version="1.0"?>
<Survey xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>US CDB GI MMPS </Name>
  <Vendor Name="TWDS" />
  <Versions>
    <Version Name="All Functions Incumbent Weighted Accouting Value" Year="2014">
      <JobMasters>
        <JobMaster JobCode="AAS000-ALL-M1" JobTitle="AAS000 Administrative Services Generalist/Multidiscipline">
          <Levels>
            <Level LevelCode="M1">
              <JobDetails>
                <JobDetail Scope="Global Revenue">
                  <PayComponents>
                    <PayComponent Id="1" Name="Base" CompanyCount="16" IncumbentCount="281" IsPercent="false">
                      <Type>Base</Type>
                      <Pays>
                        <Pay Id="100" Stat="Average" Value="65.6">
                          <StatValue>Average</StatValue>
                        </Pay>
                        <Pay Id="10" Stat="P10" Value="41">
                          <StatValue>P10</StatValue>
                        </Pay>
                        <Pay Id="25" Stat="P25" Value="51.6">
                          <StatValue>P25</StatValue>
                        </Pay>
                        <Pay Id="101" Stat="Median" Value="66.3">
                          <StatValue>Median</StatValue>
                        </Pay>
                        <Pay Id="75" Stat="P75" Value="74.2">
                          <StatValue>P75</StatValue>
                        </Pay>
                        <Pay Id="90" Stat="P90" Value="89.5">
                          <StatValue>P90</StatValue>
                        </Pay>
                      </Pays>
                    </PayComponent>
                  </PayComponents>
                </JobDetail>
              </JobDetails>
            </Level>
          </Levels>
        </JobMaster>
      </JobMasters>
      <Reports>
        <Report Id="0" Name="standard" Type="Published" DatePublished="2015-04-06T14:29:52.7826312-07:00">
          <JobDetails />
        </Report>
      </Reports>
    </Version>
  </Versions>
</Survey>

It is much larger, but this is the format. I want to dump these records into a denormalized table, containing everything from the Survey Name through Pay StatValue. I have tried many variations of the following script, but I can only correctly retrieve the Name of the Survey. Sometimes I can get the Survey Name and all StatValues concatenated as a string, all in one field like this.. US CDB GI MMPS BaseAverageP10P25MedianP75P90, and it is not useful or expected. Anyway, here is the script I'm using

--------WORKS----------------------------
DECLARE @myXML xml = (SELECT XMLData from XMLTest)
SELECT 
T.C.value('Name[1]', 'varchar(255)') as Surveys--,
--T.C.value('JobCode[1]','varchar(255)') as JobCode
FROM @myXML.nodes('(/Survey)') as T(C)

-------------DOESN'T WORK-------------------------
DECLARE @myXML xml = (SELECT XMLData from XMLTest)
SELECT 
T.C.value('Name[1]','varchar(255)') as VendorName
FROM @myXML.nodes('(/Survey/Vendor)') as T(C)

回答1:


Here's an idea for you. I've tried to shred data for some and you can do it for others.

DECLARE @myXML xml = '<?xml version="1.0"?>
<Survey xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>US CDB GI MMPS </Name>
  <Vendor Name="TWDS" />
 <Versions>
<Version Name="All Functions Incumbent Weighted Accouting Value" Year="2014">
  <JobMasters>
    <JobMaster JobCode="AAS000-ALL-M1" JobTitle="AAS000 Administrative Services Generalist/Multidiscipline">
      <Levels>
        <Level LevelCode="M1">
          <JobDetails>
            <JobDetail Scope="Global Revenue">
              <PayComponents>
                <PayComponent Id="1" Name="Base" CompanyCount="16" IncumbentCount="281" IsPercent="false">
                  <Type>Base</Type>
                  <Pays>
                    <Pay Id="100" Stat="Average" Value="65.6">
                      <StatValue>Average</StatValue>
                    </Pay>
                    <Pay Id="10" Stat="P10" Value="41">
                      <StatValue>P10</StatValue>
                    </Pay>
                    <Pay Id="25" Stat="P25" Value="51.6">
                      <StatValue>P25</StatValue>
                    </Pay>
                    <Pay Id="101" Stat="Median" Value="66.3">
                      <StatValue>Median</StatValue>
                    </Pay>
                    <Pay Id="75" Stat="P75" Value="74.2">
                      <StatValue>P75</StatValue>
                    </Pay>
                    <Pay Id="90" Stat="P90" Value="89.5">
                      <StatValue>P90</StatValue>
                    </Pay>
                  </Pays>
                </PayComponent>
              </PayComponents>
            </JobDetail>
          </JobDetails>
        </Level>
      </Levels>
    </JobMaster>
  </JobMasters>
  <Reports>
    <Report Id="0" Name="standard" Type="Published" DatePublished="2015-04-06T14:29:52.7826312-07:00">
      <JobDetails />
    </Report>
  </Reports>
</Version>
     </Versions>
     </Survey>'

   DECLARE @docH INT;
    EXEC [sys].[sp_xml_preparedocument] @docH OUTPUT, @myXML;

SELECT *
FROM OPENXML(@docH,    '/Survey/Versions/Version/JobMasters/JobMaster/Levels/Level/JobDetails/JobDetail/PayComponents/PayComponent/Pays/Pay', 3)
 WITH (Stat nvarchar(50) '@Stat',
  Value DECIMAL '@Value',
  Id INT    '@Id',
  PayComponentName NVARCHAR(50) '../../@Name',
  PayComponentId int '../../@Id',
  JobDetailScope NVARCHAR(20) '../../../../@Scope'
  )

 EXEC [sys].[sp_xml_removedocument] @docH


来源:https://stackoverflow.com/questions/29500860/xml-shredding-into-sql-server

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!