很抱歉再发一个关于这个主题的帖子,但我绝望地试图将这个xml导入数据库而没有任何成功。 这是xml:
<session xmlns="http://winscp.net/schema/session/1.0" start="2014-11-03T17:23:22.376Z">
<ls>
<destination value="/Output" />
<files>
<file>
<filename value="." />
<type value="d" />
<modification value="2014-11-03T12:17:58.000Z" />
<permissions value="rwxr-sr-x" />
</file>
<file>
<filename value="7215_SG.csv" />
<type value="-" />
<size value="1584161" />
<modification value="2014-11-03T12:06:46.000Z" />
<permissions value="rw-r--r--" />
</file>
<file>
<filename value="6171_SG.csv" />
<type value="-" />
<size value="2298481" />
<modification value="2014-11-03T12:05:13.000Z" />
<permissions value="rw-r--r--" />
</file>
</files>
<result success="true" />
</ls>
</session>
这是导入xml的sql语句:
SELECT xmldata.value('(./filename/@value)[1]', 'NVARCHAR(50)') AS szFilename, xmldata.value('(./type/@value)[1]', 'NVARCHAR(50)') AS szType, xmldata.value('(./size/@value)[1]', 'NVARCHAR(50)') AS szSize, xmldata.value('(./modification/@value)[1]', 'NVARCHAR(50)') AS szModification, xmldata.value('(./permissions/@value)[1]', 'NVARCHAR(50)') AS szPermissions FROM (SELECT CAST(x AS XML) FROM OPENROWSET(BULK 'd:\temp\Test\log.xml', SINGLE_BLOB) AS T(x)) AS T(x) CROSS APPLY x.nodes('//session/ls/files/file') AS X(xmldata);
你能帮助我并告诉我我做错了什么吗?
提前致谢!
答案 0 :(得分:1)
您只需要尊重在XML根节点上定义的XML命名空间!
<session xmlns="http://winscp.net/schema/session/1.0"
********************************************
要适应这个XML命名空间,你需要在你的XQuery中定义 - 我认为最好的方法是使用WITH XMLNAMESPACES
语句:
;WITH XMLNAMESPACES(DEFAULT 'http://winscp.net/schema/session/1.0')
SELECT
xmldata.value('(./filename/@value)[1]', 'NVARCHAR(50)') AS szFilename,
xmldata.value('(./type/@value)[1]', 'NVARCHAR(50)') AS szType,
xmldata.value('(./size/@value)[1]', 'NVARCHAR(50)') AS szSize,
xmldata.value('(./modification/@value)[1]', 'NVARCHAR(50)') AS szModification,
xmldata.value('(./permissions/@value)[1]', 'NVARCHAR(50)') AS szPermissions
FROM
(SELECT CAST(x AS XML)
FROM OPENROWSET(BULK 'd:\temp\Test\log.xml', SINGLE_BLOB) AS T(x)) AS T(x)
CROSS APPLY
x.nodes('//session/ls/files/file') AS X(xmldata);