我需要将XML文件转换为Microsoft SQL Server中的表。我们如何将XML文件中的URL转换为表数据?
答案 0 :(得分:0)
也许它会对你有所帮助:
DECLARE @xmls TABLE
(
Id INT IDENTITY(1,1),
Data XML
)
DECLARE @namespaces TABLE
(
IdXml INT
, NamespaceVal XML
)
INSERT INTO @xmls
(
Data
)
SELECT
'
<x:parent xmlns:x="http://temp.temp.org/x/x/" xmlns:y="http://temp.temp.org/y/y/" xmlns:z="http://temp.temp.org/z/z/">
<z:child1>
<val>zzz</val>
</z:child1>
<y:child2>
<val>yyy</val>
</y:child2>
<x:child3>
<val>zzz</val>
</x:child3>
</x:parent>
'
INSERT INTO @namespaces
(
IdXml,NamespaceVal
)
SELECT t.id,
t.Data.query('
for $node in /descendant::node()[namespace-uri() != ""]
return <namespace>{ namespace-uri($node) }</namespace>'
) AS nodes
FROM @xmls t
SELECT --DISTINCT
x.IdXml
, na.na.value('.', 'VARCHAR(1000)') namespace
FROM @namespaces x
CROSS APPLY x.NamespaceVal.nodes('/') AS n(n)
CROSS APPLY n.n.nodes('./namespace') AS na(na)