【问题标题】:Get value of XML column in SQL获取 SQL 中 XML 列的值
【发布时间】:2019-10-09 16:31:35
【问题描述】:

我尝试通过 SQL 访问以下 XML 的 DisplayName - 文本。使用 -SDMPackageDigest.value('(/DesiredConfigurationDigest/SoftwareUpdateBundle/Annotation/DisplayName/@Text)[1]', 'varchar(MAX)') as 'Display Name' - 只返回一个 NULL 值。这哪里错了?

<DesiredConfigurationDigest xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/07/10/DesiredConfiguration">
  <SoftwareUpdateBundle AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160" LogicalName="SUM_41a6b6d6-6976-49b5-a4c4-0121dc96189c" Version="200">
    <Annotation xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules">
      <DisplayName Text="2019-05 Servicing Stack Update for Windows 10 Version 1903 for x86-based Systems (KB4498523)" />
      <Description Text="Install this update to resolve issues in Windows. For a complete listing of the issues that are included in this update, see the associated Microsoft Knowledge Base article for more information. After you install this item, you may have to restart your computer." />
    </Annotation>
    <ConfigurationMetadata SmsUniqueIdentity="41a6b6d6-6976-49b5-a4c4-0121dc96189c" Version="200">
      <Provider SourceType="default">
        <Operation Name="Detect">
          <Parameter Name="ScanTool">
            <Property Name="ScanToolId" Value="{54F0FA43-F0DC-403D-A5AF-4B54F58C1160}" />
            <Property Name="MinCatalogVersion" Value="1" />
          </Parameter>
        </Operation>
        <Operation Name="Install">
          <Parameter Name="CommandLine">
            <Property Name="CommandLine" Value="/WUSInstaller /UpdateID:41a6b6d6-6976-49b5-a4c4-0121dc96189c" />
          </Parameter>
          <Parameter Name="RequiresExclusiveHandling">
            <Property Name="RequiresExclusiveHandling" Value="True" />
          </Parameter>
        </Operation>
      </Provider>
    </ConfigurationMetadata>
    <SupersededUpdates>
      <SoftwareUpdateReference AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160" LogicalName="SUM_41325597-7968-4eeb-8113-6411cbbe8c90" />
    </SupersededUpdates>
    <Updates>
      <SoftwareUpdateReference AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160" LogicalName="SUM_775e479e-a5ca-4530-9c06-edfd298c824a" />
    </Updates>
  </SoftwareUpdateBundle>
</DesiredConfigurationDigest>

【问题讨论】:

标签: sql sql-server xml tsql


【解决方案1】:

默认命名空间 xmlns 在 XML 中指定了两次。解决方案是保持其中一个原样,并为第二个分配别名。

SQL

-- DDL and sample data population, start
DECLARE @tbl TABLE (ID INT IDENTITY PRIMARY KEY, SDMPackageDigest XML);
INSERT INTO @tbl
VALUES
(N'<DesiredConfigurationDigest xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/07/10/DesiredConfiguration">
    <SoftwareUpdateBundle AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160"
                          LogicalName="SUM_41a6b6d6-6976-49b5-a4c4-0121dc96189c" Version="200">
        <Annotation xmlns="http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules">
            <DisplayName Text="2019-05 Servicing Stack Update for Windows 10 Version 1903 for x86-based Systems (KB4498523)"/>
            <Description Text="Install this update to resolve issues in Windows. For a complete listing of the issues that are included in this update, see the associated Microsoft Knowledge Base article for more information. After you install this item, you may have to restart your computer."/>
        </Annotation>
        <ConfigurationMetadata SmsUniqueIdentity="41a6b6d6-6976-49b5-a4c4-0121dc96189c"
                               Version="200">
            <Provider SourceType="default">
                <Operation Name="Detect">
                    <Parameter Name="ScanTool">
                        <Property Name="ScanToolId" Value="{54F0FA43-F0DC-403D-A5AF-4B54F58C1160}"/>
                        <Property Name="MinCatalogVersion" Value="1"/>
                    </Parameter>
                </Operation>
                <Operation Name="Install">
                    <Parameter Name="CommandLine">
                        <Property Name="CommandLine"
                                  Value="/WUSInstaller /UpdateID:41a6b6d6-6976-49b5-a4c4-0121dc96189c"/>
                    </Parameter>
                    <Parameter Name="RequiresExclusiveHandling">
                        <Property Name="RequiresExclusiveHandling" Value="True"/>
                    </Parameter>
                </Operation>
            </Provider>
        </ConfigurationMetadata>
        <SupersededUpdates>
            <SoftwareUpdateReference AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160"
                                     LogicalName="SUM_41325597-7968-4eeb-8113-6411cbbe8c90"/>
        </SupersededUpdates>
        <Updates>
            <SoftwareUpdateReference AuthoringScopeId="Site_54F0FA43-F0DC-403D-A5AF-4B54F58C1160"
                                     LogicalName="SUM_775e479e-a5ca-4530-9c06-edfd298c824a"/>
        </Updates>
    </SoftwareUpdateBundle>
</DesiredConfigurationDigest>');
-- DDL and sample data population, end

;WITH XMLNAMESPACES (DEFAULT 'http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/07/10/DesiredConfiguration', 
    'http://schemas.microsoft.com/SystemsCenterConfigurationManager/2009/06/14/Rules' as ns2)
SELECT ID
    , col.value('@Text','VARCHAR(200)') AS Result
FROM @tbl tbl
    CROSS APPLY tbl.SDMPackageDigest.nodes('/DesiredConfigurationDigest/SoftwareUpdateBundle/ns2:Annotation/ns2:DisplayName') AS tab(col)

输出

+----+----------------------------------------------------------------------------------------------+
| ID |                                            Result                                            |
+----+----------------------------------------------------------------------------------------------+
|  1 | 2019-05 Servicing Stack Update for Windows 10 Version 1903 for x86-based Systems (KB4498523) |
+----+----------------------------------------------------------------------------------------------+

【讨论】:

    【解决方案2】:

    问题出在不存在的 xml 命名空间中。如果您从 xml 中删除 xmlns 节点,它将正常工作。如果您打算在 xml 文档中使用 xmlns,它应该是有效的。

    【讨论】:

    • 在大多数情况下,我们是 XML 的消费者,必须按原样处理它。您对 “删除命名空间” 的建议是一种错误的方法。最好使用不同的前缀来处理命名空间。
    • 感谢您的评论,您是对的!我从你那里学到了一些东西,谢谢。
    猜你喜欢
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-21
    相关资源
    最近更新 更多