将名称从多级XML提取到Sql列

时间:2017-06-09 10:14:24

标签: sql sql-server xml xml-parsing sqlxml

有人可以帮我弄清楚如何在下面提取XML结构的名称。我所追求的是一个平面表,其中包含每个数据项的位置。

前几个将处于高位

BusinessID
Date Submitted
E-mail etc

然而,当我得到问题时,我需要完整的位置。

Questions.Questions1.Questions11.Questions111

所以表格看起来像

BusinessID
Date Submitted
E-mail
Questions.Questions1.Questions11.Questions111
Questions.Questions1.Questions12.Questions121
Questions.Questions1.Questions12.Questions122
Questions.Questions1.Questions12.Questions123
Questions.Questions1.Questions12.Other
Questions.Questions1.Questions13.Questions131
Questions.Questions1.Questions13.Questions132

这是我的XML:

<AdapterItem xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas/Data">
  <Attributes>
    <Attribute>
      <Name>BusinessID</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">?????</Value>
    </Attribute>
    <Attribute>
      <Name>Date Submitted</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:dateTime">?????</Value>
    </Attribute>
    <Attribute>
      <Name>E-mail</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">?????</Value>
    </Attribute>
    <Attribute>
      <Name>Language</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">English (United Kingdom)</Value>
    </Attribute>
    <Attribute>
      <Name>Type</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">??????????</Value>
    </Attribute>
    <Attribute>
      <Name>Submission Channel</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">Web</Value>
    </Attribute>
    <Attribute>
      <Name>Anonymous</Name>
      <Value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string">false</Value>
    </Attribute>
    <Attribute>
      <Name>Questions</Name>
      <Value i:type="AdapterItem">
        <Attributes>
          <Attribute>
            <Name>Questions1</Name>
            <Value i:type="AdapterItem">
              <Attributes>
                <Attribute>
                  <Name>Questions11</Name>
                  <Value i:type="AdapterItem">
                    <Attributes>
                      <Attribute>
                        <Name>Questions111</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">Questions111 Answer</Value>
                      </Attribute>
                    </Attributes>
                  </Value>
                </Attribute>
                <Attribute>
                  <Name>Questions12</Name>
                  <Value i:type="AdapterItem">
                    <Attributes>
                      <Attribute>
                        <Name>Questions121</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">false</Value>
                      </Attribute>
                      <Attribute>
                        <Name>Questions122</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">false</Value>
                      </Attribute>
                      <Attribute>
                        <Name>Questions123</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">false</Value>
                      </Attribute>
                      <Attribute>
                        <Name>Other</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">false</Value>
                      </Attribute>
                    </Attributes>
                  </Value>
                </Attribute>
                <Attribute>
                  <Name>Questions13</Name>
                  <Value i:type="AdapterItem">
                    <Attributes>
                      <Attribute>
                        <Name>Questions131</Name>
                        <Value xmlns:d13p1="http://www.w3.org/2001/XMLSchema" i:type="d13p1:string">TEST business</Value>
                      </Attribute>
                      <Attribute>
                        <Name>Questions132</Name>
                        <Value i:type="AdapterItem">
                          <Attributes>
                            <Attribute>
                              <Name>Questions1321</Name>
                              <Value xmlns:d16p1="http://www.w3.org/2001/XMLSchema" i:type="d16p1:string">Damaged</Value>
                            </Attribute>
                            <Attribute>
                              <Name>Questions1322</Name>
                              <Value i:type="AdapterItem">
                                <Attributes>
                                  <Attribute>
                                    <Name>Questions13221</Name>
                                    <Value xmlns:d19p1="http://www.w3.org/2001/XMLSchema" i:type="d19p1:string">TEST</Value>
                                  </Attribute>
                                  <Attribute>
                                    <Name>Questions13222</Name>
                                    <Value xmlns:d19p1="http://www.w3.org/2001/XMLSchema" i:type="d19p1:string">Metal</Value>
                                  </Attribute>
                                  <Attribute>
                                    <Name>Questions13223</Name>
                                    <Value xmlns:d19p1="http://www.w3.org/2001/XMLSchema" i:type="d19p1:string">true</Value>
                                  </Attribute>
                                  <Attribute>
                                    <Name>Questions13224</Name>
                                    <Value xmlns:d19p1="http://www.w3.org/2001/XMLSchema" i:type="d19p1:string">false</Value>
                                  </Attribute>
                                  <Attribute>
                                    <Name>Questions13225</Name>
                                    <Value xmlns:d19p1="http://www.w3.org/2001/XMLSchema" i:type="d19p1:string">false</Value>
                                  </Attribute>
                                </Attributes>
                              </Value>
                            </Attribute>
                          </Attributes>
                        </Value>
                      </Attribute>
                    </Attributes>
                  </Value>
                </Attribute>
              </Attributes>
            </Value>
          </Attribute>
        </Attributes>
      </Value>
    </Attribute>
    </Attributes>
</AdapterItem>

我一直试图解决这个问题,但我无法弄清楚如何获取名称而不是值

DECLARE @xml as xml

SET @xml = (SELECT data);

WITH Xml_CTE AS
(
    SELECT
        CAST('/' + node.value('fn:local-name(.)',
            'varchar(100)') AS varchar(100)) AS name,
        node.query('*') AS children
    FROM @xml.nodes('/*') AS roots(node)

    UNION ALL

    SELECT
        CAST(x.name + '/' + 
            node.value('fn:local-name(.)', 'varchar(100)') AS varchar(100)),
        node.query('*') AS children
    FROM Xml_CTE x
    CROSS APPLY x.children.nodes('*') AS child(node)
)
SELECT DISTINCT name
FROM Xml_CTE
OPTION (MAXRECURSION 1000)

0 个答案:

没有答案