SQL Server XML 查询:查询多个同名子元素

2021-09-10 00:00:00 xml xpath tsql xquery sql-server

在上一个问题中,我想知道如何使用 SQL 根据标识符JOIN 不同的 XML 元素.我得到了几个不错的解决方案,你可以看到 这里.

In a previous question I wanted to know how I can use SQL to JOIN different XML elements based on an identifier. I was provided with several nice solutions as you can see here.

现在我正在根据我的实际数据调整此解决方案.不幸的是,我偶然发现了我在链接问题中提供的最小可行示例中不存在的新障碍.在我的实际数据中,我还有几个同名的子元素.请参阅以下示例数据中的元素 .

Now I am in the process of adapting this solution to my actual data. Unfortunately I stumbled upon a new obstacle that was not present in the minimum viable example I provided in the linked question. In my actual data I also have several child elements of the same name. See the element <subElement> in the following example data.

<xml>
    <dataSetData>
        <text>ABC</text>
    </dataSetData>
    <generalData>
        <id>123</id>
        <text>text data</text>
        <subElement>
            <subData>sub example data AAA</subData>
        </subElement>
        <subElement>
            <subData>sub example data BBB</subData>
        </subElement>
    </generalData>
    <generalData>
        <id>456</id>
        <text>text data 2</text>
        <subElement>
            <subData>sub example data CCC</subData>
        </subElement>
    </generalData>
    <specialData>
        <id>123</id>
        <text>special data text</text>
    </specialData>
    <specialData>
        <id>456</id>
        <text>special data text 2</text>
    </specialData>
</xml>

预期结果应如下所示.

DataSetData | GeneralDataID | GeneralDataText | subData              | SpecialDataTest
ABC         | 123           | text data       | sub example data AAA | special data text
ABC         | 123           | text data       | sub example data BBB | special data text
ABC         | 456           | text data  2    | sub example data CCC | special data text 2

目前的解决方案(不考虑数据)如下(取自这里):

The current solution (without considering the <subElement> data) is as follows (taken from here):

SELECT TheXml.value('(/xml/dataSetData/text/text())[1]', 'VARCHAR(20)') AS DataSetData
     ,B.*
    , sp.value('(id/text())[1]', 'INT') AS SpecialDataID 
    , sp.value('(text/text())[1]', 'VARCHAR(30)') AS SpecialDataTest
INTO dbo.TestResult4
FROM dbo.TestXml
CROSS APPLY TheXml.nodes('/xml/generalData') AS A(g)
CROSS APPLY(SELECT g.value('(id/text())[1]', 'INT') AS GeneralDataID 
                 , g.value('(text/text())[1]', 'VARCHAR(30)') AS GeneralDataText) B
OUTER APPLY TheXml.nodes('/xml/specialData[id=sql:column("B.GeneralDataID")]') AS special(sp);

推荐答案

好的,这很简单...

SELECT TheXml.value('(/xml/dataSetData/text/text())[1]', 'VARCHAR(20)') AS DataSetData
    , B.*
    , se.value('(subData/text())[1]','varchar(100)') AS SubData 
    , sp.value('(id/text())[1]', 'INT') AS SpecialDataID 
    , sp.value('(text/text())[1]', 'VARCHAR(30)') AS SpecialDataTest
FROM dbo.TestXml
CROSS APPLY TheXml.nodes('/xml/generalData') AS A(g)
CROSS APPLY(SELECT g.value('(id/text())[1]', 'INT') AS GeneralDataID 
                 , g.value('(text/text())[1]', 'VARCHAR(30)') AS GeneralDataText) B
OUTER APPLY A.g.nodes('subElement') C(se)
OUTER APPLY TheXml.nodes('/xml/specialData[id=sql:column("B.GeneralDataID")]') AS D(sp);

相关文章