OPENXML mit xmlns:dt
Benutze OPENXML um dt-element in MSSQL 2005.
Wie kann ich bekommen xmlns:dt-element in der xml? Zum Beispiel, erhalten Sie ein ResultSet, das aus zwei Zeilen, die Liste der Produkt-id und Länderkennung.
121403 GBR
121403 USA
declare @xmldata xml
set @xmldata =
'<?xml version="1.0"?>
<data xmlns="http://www.aaa.com/master_browse_response" xmlns:dt="http://www.aaa.com/DataTypes">
<products>
<product>
<product_id><![CDATA[121403]]></product_id>
<countries>
<dt:country>GBR</dt:country>
<dt:country>USA</dt:country>
</countries>
</product>
</products>
</data>'
DECLARE @hDoc int, @rootxmlns varchar(100)
SET @rootxmlns = '<root xmlns:hm="http://www.aaa.com/master_browse_response"/>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns
SELECT *
FROM OPENXML(@hDoc, '//hm:product',2)
WITH ([hm:product_id] int , [hm:countries] varchar(100))
--clean up
EXEC sp_xml_removedocument @hDoc
Hier ist eine Lösung, die ich kenne mithilfe xmlEdgeTable, aber ich bin auf der Suche nach einer besseren Lösung.
DECLARE @hDoc int, @rootxmlns varchar(100)
SET @rootxmlns = '<root xmlns:hm="http://www.aaa.com/master_browse_response"/>'
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xmldata, @rootxmlns
CREATE TABLE #xmlEdgeTable
(
id int,
parentid int,
localname varchar(20),
[text] varchar(20)
)
INSERT INTO #xmlEdgeTable
SELECT id, parentid,localname, cast([text] as varchar(20))
FROM OPENXML(@hDoc, '//hm:product',2)
SELECT t6.text, t2.text FROM #xmlEdgeTable AS t1 INNER JOIN
#xmlEdgeTable AS t2 ON t1.id = t2.parentid INNER JOIN
#xmlEdgeTable AS t3 ON t3.id = t1.parentid INNER JOIN
#xmlEdgeTable AS t4 ON t4.id = t3.parentid INNER JOIN
#xmlEdgeTable AS t5 ON t4.id = t5.parentid INNER JOIN
#xmlEdgeTable AS t6 ON t5.id = t6.parentid
WHERE t1.localname = 'country' and t5.localname ='product_id'
--clean up
EXEC sp_xml_removedocument @hDoc
DROP TABLE #xmlEdgeTable
Du musst angemeldet sein, um einen Kommentar abzugeben.
Gibt es einen bestimmten Grund, dass Sie brauchen, um die Verwendung von OPENXML zu tun? Sie können ganz einfach die Informationen bekommen, die mit einem XQUERY-im Jahr 2005 so:
Den neueren XQUERY-Funktionen sind eine viel bessere Wahl für die Lösung Ihres Problems.
BEARBEITEN:
Die gleiche Lösung mit OPENXML wäre:
Für ein kleines dataset, es gibt keinen großen Unterschied zwischen XQuery und OPENXML
Hier sind die Ergebnisse zu analysieren 6,5 MB xml-Datei zu bekommen 27,615 Zeilen: