从 SQL Server 2012 中的 XML 列中提取属性

Extracting attributes from XML column in SQL Server 2012

我有一个名为 MetaXML 的列,其中存储了 XML。我只想提取与 URLpanel 标签相关的信息。如何访问 node/element 直到 URLPanel 标签。我还想分离出 Cap、CaptionText、Height、HeightType、ID 和 URL(全部包含在 URLPanel 中)。那可能吗?

注意:我已经删除了第一行的架构信息...

<DASHBOARD>
<COLUMNS>
<COLUMN Width="19" WidthType="Percent">
  <PANELS>
    <ACTIONLISTPANEL Cap="8fdd05da-c6b9-41c3-9e4d-5b661c3e134a" 
CaptionText="My Action List" Height="14" HeightType="Percent" 
ID="CL_Front9.2_c0_r0" />
    <PROCESSPANEL Cap="11e934f8-1aed-4fd3-ad95-42049343a390" CaptionText="My 
Processes" Height="19" HeightType="Percent" ID="CL_Front9.2_c0_r1">
      <APPOBJECTS>
        <APPOBJECT AppObjectID="CL_WfMyMattersActList" ID="8a690a0f-268c- 
4eb0-8536-6ac09605b7f8" Type="Process" />
        <APPOBJECT AppObjectID="CL_ModifyTimeNarrative" ID="d2f320e9-2797- 
4631-b63a-5bdcb241d633" Type="Process" />
        <APPOBJECT AppObjectID="Invoice Inquiry" ID="7749f30c-acdf-4c09- 
b841-60026e274284" Type="Dashboard" />
      </APPOBJECTS>
    </PROCESSPANEL>
    <DASHBOARDENTRY Cap="13d4cd47-a3cf-4222-b03b-932bd8ff6850" 
CaptionText="Inquiry" DBID="CliMattInquiry2" Height="50" 
HeightType="Percent" ID="CL_Front9.2_c0_r2" />
    <REPORTPANEL Cap="9596ced2-76d2-4ebb-a7a3-4f2bd2450fde" CaptionText="My 
Details" Height="17" HeightType="Percent" ID="CL_Front9.2_c0_r3">
      <VIEWS CurrentViewID="CL_TkprStaticUser">
        <VIEW Cap="5f99bd4b-1509-4a65-a54c-07bbc444aceb" CaptionText="My 
Details" ReportLayoutID="CL_TkprStaticUser" Type="Report" />
      </VIEWS>
    </REPORTPANEL>
  </PANELS>
</COLUMN>
<COLUMN Width="77" WidthType="Percent">
  <PANELS>
    <URLPANEL Cap="de940bd4-80f8-47f4-b721-76eed63c650f" CaptionText="Fee 
Earner Dashboard" Height="100" HeightType="Percent" ID="CL_Front9.2_c1_r0" 

Url="http://ccdc1sql119/ReportServer_SQLR_FINDW_PRD1/Pages/ReportViewer.aspx?%2fDWR%2fMIP+Phase+3%2fMIP_Landing_Indiv&amp;rs:Command=Render&amp;rc:Parameters=Collapsed" />
  </PANELS>
 </COLUMN>
 </COLUMNS>
 <SHORTLIST />
 </DASHBOARD>

上面的 XML 很小,但我似乎无法提取 URLPanel 标签。我创建了以下 SQL 以尝试提取 URL 面板标签:

DECLARE @DataID UNIQUEIDENTIFIER
SET @DataID = 'AEFFF874-5980-4340-92C1-ED6B292AA667'

BEGIN
SET NOCOUNT ON;

IF OBJECT_ID('tempdb..#TempXML') IS NOT NULL 
     DROP TABLE #TempXML

DECLARE @X XML

SET @X = (SELECT D.MetaXML AS 'MetaXML' 
          FROM NxFWKAppObjectData AS D 
          WHERE NxFWKAppObjectDataID = @DataID)

DECLARE @iX INT
EXEC sp_xml_preparedocument @ix output, @x

CREATE TABLE #TempXML (URLPanel NVARCHAR(MAX))

INSERT INTO #TempXML(URLPanel)
    SELECT
        URLPanel
    FROM 
        OPENXML(@iX, '//DASHBOARD/COLUMNS/COLUMN/PANELS/URLPANEL', 2) 
    WITH 
        (URLPanel NVARCHAR(MAX))

SELECT *
FROM #TempXML AS DT

EXEC sp_xml_removedocument @iX

DROP TABLE #TempXML
END

我建议使用 内置 本机 XQuery 支持(而不是存在内存泄漏等问题的遗留 OPENXML 方法)。

给定您的 @X 变量 - 试试这个:

 SELECT 
    Cap = XC.value('@Cap', 'varchar(50)'),
    CaptionText = XC.value('@CaptionText', 'varchar(250)'),
    Height = XC.value('@Height', 'int'),
    HeightType = XC.value('@HeightType', 'varchar(50)'),
    ID = XC.value('@ID', 'varchar(50)'),
    Url = XC.value('@Url', 'varchar(500)')
 FROM
    @X.nodes('/DASHBOARD/COLUMNS/COLUMN/PANELS/URLPANEL') AS XT(XC)

这应该 return 您正在寻找的数据位。