2012-08-27 38 views
0

我試圖使用本指南讀取SQL Server中的XML文件。在命名空間中讀取SQL Server上的Xml

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

工作正常,但我有一個XML文件,命名空間和doesn't的工作原理是代碼命名空間。

一些解決方案?


感謝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) 

有效,但totalImpuestosTraslados的值爲NULL。

莫內達totalImpuestosTraslados

美元NULL

回答

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)