带有 SQL 的 XML 节点

XML nodes with SQL(带有 SQL 的 XML 节点)

本文介绍了带有 SQL 的 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XML 数据,并希望按如下方式在 KS 中获取数据:

I have the following XML data and would like to get data inside KS as row as follows:

<DW>
  <KS>
    <KeyInfo Name="IlluSetting">
      <KeyTypeValue>Text</KeyTypeValue>
      <ValueString>yDipol90</ValueString>
    </KeyInfo>
     <KeyInfo Name="IlluSetting2">
      <KeyTypeValue>Text</KeyTypeValue>
      <ValueString>yDipol</ValueString>
    </KeyInfo>
  </KS>
  <MDESC>Tx [mrad]</MDESC>
   <MNUMBER>0.12102</MNUMBER>
</DW>
<DW>
  <KS>
    <KeyInfo Name="IlluSetting3">
      <KeyTypeValue>Text</KeyTypeValue>
      <ValueString>yDipol80</ValueString>
    </KeyInfo>
  </KS>
  <MDESC>Ty [mrad]</MDESC>
  <MNUMBER>0.12102</MNUMBER>
</DW>

有什么办法可以得到一个具有以下输出的表格:

Is there any way to get a Table with the following output:

Name            ValueString     Name            ValueString
-----------------------------------------------------------
IlluSetting     yDipol90        IlluSetting2    yDipol
IlluSetting3    yDipol80    

表示<KS>...</KS>里面的数据会排成一行显示

which means that the data inside <KS>... </KS> will be shown in a row

非常感谢

推荐答案

请尝试以下解决方案.我们在这里所做的称为粉碎,即将 XML 转换为矩形/关系格式.

Please try the following solution. What we are doing here is called shredding, i.e. converting XML into rectangular/relational format.

我正在拍摄,因为没有提供DDL 和样本数据群.

I am shooting from the hip because DDL and sample data population were not provided.

由于缺少根元素,提供的 XML 格式不正确,但 SQL Server 允许处理 XML 片段.

The provided XML is not well-formed due to missing root element, but SQL Server allows to handle XML fragments.

我们正在使用 XQuery 及其 .nodes().value() 方法.

We are using XQuery and its .nodes() and .value() methods.

SQL,方法 #1

-- DDL and sample data population, start
DECLARE @xml XML =
N'<DW>
    <KS>
        <KeyInfo Name="IlluSetting">
            <KeyTypeValue>Text</KeyTypeValue>
            <ValueString>yDipol90</ValueString>
        </KeyInfo>
        <KeyInfo Name="IlluSetting2">
            <KeyTypeValue>Text</KeyTypeValue>
            <ValueString>yDipol</ValueString>
        </KeyInfo>
    </KS>
    <MDESC>Tx [mrad]</MDESC>
    <MNUMBER>0.12102</MNUMBER>
</DW>
<DW>
    <KS>
        <KeyInfo Name="IlluSetting3">
            <KeyTypeValue>Text</KeyTypeValue>
            <ValueString>yDipol80</ValueString>
        </KeyInfo>
    </KS>
    <MDESC>Ty [mrad]</MDESC>
    <MNUMBER>0.12102</MNUMBER>
</DW>';
-- DDL and sample data population, end

SELECT c.value('KeyInfo[1]/@Name', 'VARCHAR(30)') AS name1
    , c.value('(KeyInfo[1]/ValueString/text())[1]', 'VARCHAR(30)') AS ValueString1
    , COALESCE(c.value('KeyInfo[2]/@Name', 'VARCHAR(30)'), '') AS name2
    , COALESCE(c.value('(KeyInfo[2]/ValueString/text())[1]', 'VARCHAR(30)'), '') AS ValueString2
FROM @xml.nodes('/DW/KS') AS t(c);

输出

+--------------+--------------+--------------+--------------+
|    name1     | ValueString1 |    name2     | ValueString2 |
+--------------+--------------+--------------+--------------+
| IlluSetting  | yDipol90     | IlluSetting2 | yDipol       |
| IlluSetting3 | yDipol80     |              |              |
+--------------+--------------+--------------+--------------+

SQL,方法 #2

DECLARE @CrLf CHAR(2) = CHAR(13) + CHAR(10)
   , @tokenCounter INT
   , @i INT = 1;

-- Calculate max number of tokens in the <KS>
SET @tokenCounter = (SELECT MAX(c.value('count(KeyInfo)', 'INT'))
FROM @xml.nodes('/DW/KS') AS t(c));

DECLARE @SQL NVARCHAR(MAX) = 'SELECT ';

WHILE @i <= @tokenCounter BEGIN
    SET @SQL += IIF(@i>1,', ','') + 'COALESCE(c.value(''KeyInfo[' + CAST(@i AS VARCHAR(3)) + ']/@Name'', ''VARCHAR(30)''), '''') AS NAME' + CAST(@i AS VARCHAR(3)) + @CrLf
    SET @SQL += ', COALESCE(c.value(''(KeyInfo[' + CAST(@i AS VARCHAR(3)) + ']/ValueString/text())[1]'', ''VARCHAR(30)''), '''') AS ValueString' + CAST(@i AS VARCHAR(3)) + @CrLf

    SET @i += 1
END

SET @SQL += 'FROM @xml.nodes(''/DW/KS'') AS t(c);';

-- just to see it
PRINT @sql;

-- we are ready at this point
EXEC sp_executesql @stmt = @SQL, @params = N'@xml xml', @xml = @xml;

这篇关于带有 SQL 的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:带有 SQL 的 XML 节点

基础教程推荐