帮助!
我有一个由WinSCP生成的XML日志文件,我想根据它的内容触发警报。我无法控制XML的格式化。
我试图使用xQuery来读取值,但没有运气。我得到的只是0行的列标题。我也尝试过OPENXML和VBA ImportXML,但两种方法都返回null。
DECLARE @XML XML =
'<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="ftpuser@ftphost.com" start="2017-11-09T18:00:09.051Z">
<upload>
<filename value="e:\MyUploadFile.txt" />
<destination value="/HostFolder/MyUploadFile.txt" />
<result success="true" />
</upload>
<touch>
<filename value="/HostFolder/MyUploadFile.txt" />
<modification value="2017-11-09T18:00:08.000Z" />
<result success="true" />
</touch>
</session>
'
SELECT
Sess.Upload.value('../filename[1]','varchar(250)') [Filename]
,Sess.Upload.value('../destination[1]','varchar(250)') [Destination]
,Sess.Upload.value('../result[1]','varchar(250)') [Result]
FROM
@XML.nodes ('/session/upload') AS Sess(Upload)
答案 0 :(得分:0)
你操纵过你的XML吗?在第二行.../session/1.0
和name
后,引号丢失。
此XML具有您必须声明的默认命名空间。我不知道,你期望输出什么,但我的魔法水晶球告诉我,它可能是这样的:
DECLARE @XML XML =
'<?xml version="1.0" encoding="UTF-8"?>
<session xmlns="http://winscp.net/schema/session/1.0" name="ftpuser@ftphost.com" start="2017-11-09T18:00:09.051Z">
<upload>
<filename value="e:\MyUploadFile.txt" />
<destination value="/HostFolder/MyUploadFile.txt" />
<result success="true" />
</upload>
<touch>
<filename value="/HostFolder/MyUploadFile.txt" />
<modification value="2017-11-09T18:00:08.000Z" />
<result success="true" />
</touch>
</session>
';
WITH XMLNAMESPACES(DEFAULT 'http://winscp.net/schema/session/1.0')
SELECT
Sess.Upload.value('local-name(.)','nvarchar(max)') AS FileAction
,Sess.Upload.value('(filename/@value)[1]','varchar(250)') [Filename]
,Sess.Upload.value('(destination/@value)[1]','varchar(250)') [Destination]
,Sess.Upload.value('(modification/@value)[1]','varchar(250)') [Modification]
,Sess.Upload.value('(result/@success)[1]','varchar(250)') [Result]
FROM @XML.nodes ('/session/*') AS Sess(Upload);
答案 1 :(得分:0)
您似乎在命名空间的末尾省略了双引号,因此您的XML实际上无效。
您必须declare the namespace。像:
WITH XMLNAMESPACES (DEFAULT 'http://winscp.net/schema/session/1.0')
../
错了。它应该是./
。