Help!
I have an XML log file generated by WinSCP, and I'd like to trigger an alert based on it's contents. I have no control on how the XML is formatted.
I'm trying to use xQuery to read the values, but no luck. All I get are column heading with 0 rows. I've also tried OPENXML and VBA ImportXML, but both methods return nulls.
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)
Did you manipulate your XML? There is a quote missing after .../session/1.0
and name
in the second line.
This XML has a default namespace which you have to declare. I do not know, what you expect as output, but my magic crystal ball tells me, that it might be something like this:
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);
It seems that you have omitted double-quote at the end of the namespace, so your XML is effectively invalid.
You have to declare the namespace . Like:
WITH XMLNAMESPACES (DEFAULT 'http://winscp.net/schema/session/1.0')
../
is wrong. It should be ./
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.