使用命名空间在SQL Server上读取Xml

问题描述 投票:0回答:3

我尝试使用本指南在SQL Server中读取XML文件。

http://blog.sqlauthority.com/2009/02/13/sql-server-simple-example-of-reading-xml-file-using-t-sql/

工作正常,但我有一个带有命名空间的XML文件,并且这个代码不能用于命名空间。

一些解决方案


谢谢Remus。现在我有了这段代码

 DECLARE @MyXML XML
SET @MyXML = '<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3"   Moneda="USD">
    <cfdi:Impuestos totalImpuestosRetenidos="0.00" totalImpuestosTrasladados="1143.06">
    </cfdi:Impuestos>
</cfdi:Comprobante> ' 

;with xmlnamespaces('http://www.sat.gob.mx/cfd/3' as cfdi)
SELECT
a.b.value('@Moneda','varchar(100)') Moneda,
a.b.value('Impuestos[1]/@totalImpuestosTrasladados','float') totalImpuestosTraslados
FROM @MyXML.nodes('cfdi:Comprobante') a(b)

Works,但totalImpuestosTraslados的值为NULL。

总货币税

USD NULL

sql-server xml
3个回答
2
投票

使用WITH XMLNAMESPACES。如果此信息不足,请发布详细信息(您尝试了什么,您得到了什么错误)。


0
投票
DECLARE @MyXML XML
SET @MyXML = '<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3"   Moneda="USD">
    <cfdi:Impuestos totalImpuestosRetenidos="0.00" totalImpuestosTrasladados="1143.06">
    </cfdi:Impuestos>
</cfdi:Comprobante> ' 

;with xmlnamespaces('http://www.sat.gob.mx/cfd/3' as cfdi)
SELECT
a.b.value('@Moneda','varchar(100)') Moneda,
a.b.value('cfdi:Impuestos[1]/@totalImpuestosTrasladados','float') totalImpuestosTraslados
FROM @MyXML.nodes('cfdi:Comprobante') a(b)

0
投票

“Impuestos”也有cfdi作为命名空间,所以你也必须包含它

DECLARE @MyXML XML
SET @MyXML = '<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3"   Moneda="USD">
<cfdi:Impuestos totalImpuestosRetenidos="0.00" totalImpuestosTrasladados="1143.06">
</cfdi:Impuestos>
</cfdi:Comprobante> ' 

;with xmlnamespaces('http://www.sat.gob.mx/cfd/3' as cfdi)
SELECT
a.b.value('@Moneda','varchar(100)') Moneda,
a.b.value('<b>/cfdi:</b>Impuestos[1]/@totalImpuestosTrasladados','float') totalImpuestosTraslados
FROM @MyXML.nodes('cfdi:Comprobante') a(b)
© www.soinside.com 2019 - 2024. All rights reserved.