sql-server - 在 TSQL 中读取 XML 时属性上的 NULL 值
问题描述
读取此 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>
解决方案
您需要使用命名空间为属性添加前缀,如下所示:
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)
推荐阅读
- python - 打印子进程的标准输出管道忽略最后一行
- highcharts - 如何将highcharts工具提示定位在图表上方,外部:true
- angular - 打字稿中的时间跨度
- c - ARM64系统中不同大小的指针投射警告
- java - 定义最小子和代码的复杂度
- file - Dart Flutter Socket 是否可以通过套接字接收文件?
- c++ - 为什么捕获列表中的 [[maybe_unused]] 不可能?
- python - 熊猫系列对象:将字符串切成相等的块
- laravel - 使用 eloquent 获取特定列的最新数据
- kubernetes - 在 Redhat Linux 7.9 服务器上为 jenkins 应用程序在 k8s v19 上配置 haproxy ingress