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

时间:2018-12-06 16:27:23

标签: sql sql-server xml element xml-attribute

我有一列名为MetaXML的列,其中存储了XML。我只想提取与URLpanel标签有关的信息。我如何访问节点/元素直到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来尝试提取URLPanel标记:

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

1 个答案:

答案 0 :(得分:1)

我建议使用内置本机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)

希望这应该返回您要查找的数据。