在TSQL中读取XML时,属性值为NULL

时间:2018-06-22 15:45:18

标签: sql-server xml tsql

读取此xml文件时,我尝试返回3个属性。 3个中的2个正在返回期望值。我不明白为什么我不能获得ID返回值。它总是返回NULL。如何获得rid1和rid2的正确值

DECLARE @xml xml
SELECT @xml = BulkColumn
FROM OPENROWSET(BULK 'C:\data\workbook.xml', SINGLE_BLOB) x;

WITH XMLNAMESPACES (default 
'http://schemas.openxmlformats.org/spreadsheetml/2006/main' ,                    
'http://schemas.openxmlformats.org/officeDocument/2006/relationships' as a,
'http://schemas.openxmlformats.org/markup-compatibility/2006' as b,
'http://schemas.microsoft.com/office/spreadsheetml/2010/11/main' as c,                   
'http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac' as d)

SELECT
 doc.col.value('@name', 'nvarchar(10)') sheet
,doc.col.value('@id', 'nvarchar(max)') rid 
,doc.col.value('@sheetId', 'int') id 
FROM @xml.nodes('*:workbook/sheets/sheet') doc(col)

这是XML文件

    <workbook xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" 
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" mc:Ignorable="x15">
  <fileVersion appName="xl" lastEdited="7" lowestEdited="7" rupBuild="18431" />
  <workbookPr defaultThemeVersion="166925" />
  <mc:AlternateContent xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
    <mc:Choice Requires="x15">
      <x15ac:absPath xmlns:x15ac="http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac" url="C:\data\" />
    </mc:Choice>
  </mc:AlternateContent>
  <bookViews>
    <workbookView xWindow="0" yWindow="0" windowWidth="21576" windowHeight="7968" />
  </bookViews>
  <sheets>
    <sheet name="Sheet1" sheetId="1" r:id="rId1" />
    <sheet name="Sheet2" sheetId="2" r:id="rId2" />
  </sheets>
  <calcPr calcId="171027" />
  <fileRecoveryPr repairLoad="1" />
  <extLst>
    <ext xmlns:x15="http://schemas.microsoft.com/office/spreadsheetml/2010/11/main" uri="{140A7094-0E35-4892-8432-C4D2E57EDEB5}">
      <x15:workbookPr chartTrackingRefBase="1" />
    </ext>
  </extLst>
</workbook>

results

2 个答案:

答案 0 :(得分:1)

您需要使用以下名称空间为属性添加前缀:

SELECT
 doc.col.value('@name', 'nvarchar(10)') sheet
,doc.col.value('@a:id', 'nvarchar(max)') rid 
,doc.col.value('@sheetId', 'int') id 
FROM @xml.nodes('*:workbook/sheets/sheet') doc(col)

答案 1 :(得分:1)

在T-SQL中为命名空间提供与XML中相同的别名,并在引用中使用命名空间别名:

WITH XMLNAMESPACES (DEFAULT 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
                    'http://schemas.openxmlformats.org/officeDocument/2006/relationships' AS r,
                    'http://schemas.openxmlformats.org/markup-compatibility/2006' AS mc,
                    'http://schemas.microsoft.com/office/spreadsheetml/2010/11/main' AS x15,
                    'http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac' AS x15ac)
SELECT doc.col.value('@name', 'nvarchar(10)') sheet,
       doc.col.value('@r:id', 'nvarchar(max)') rid,
       doc.col.value('@sheetId', 'int') id
FROM @xml.nodes('*:workbook/sheets/sheet') doc(col);