mIRC Home    About    Download    Register    News    Help

Print Thread
XML -> XSLT -> plain text output;not working #267638 26/08/20 04:33 PM
Joined: Feb 2015
Posts: 105
kap Offline OP
Vogon poet
OP Offline
Vogon poet
Joined: Feb 2015
Posts: 105
I wish to obtain the weather via OpenWeatherMap.org and I can't get past this problem.

I am loading the XML returned by the http link into a MSXML2.DOMDocument.6.0 object via a COM connection.
I am loading the XSLT stylesheet via a variable into a MSXML2.DOMDocument.6.0 object via a COM connection.

Then lastly, I am running a transformNode function and hope to pass the XSLT object to the XML object.
It's not working...

I expect to see it output something like:
`City: Flushing - Country: NL - Temperature: 18.23celsius`

To run the code, simply type `/t1`

Where am I going wrong?

edit: I know there's an API key in here. You can use it for testing, I'll remove in a couple of days...

Code
alias -l url_encode return $regsubex($1, /([\W\s])/Sg, $iif(\t == $chr(32), +, $+(%, $base($asc(\t), 10, 16, 2))))
alias objStylesheet {
  var %error, %xslt <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="text" indent="no" omit-xml-declaration="yes" /><xsl:template match="/">City: <xsl:value-of select="/current/city/@name" /> - Country: <xsl:value-of select="/current/city/country" /> - Temperature: <xsl:value-of select="/current/temperature/@value" /><xsl:value-of select="/current/temperature/@unit" /></xsl:template></xsl:stylesheet>
  if ($com(XSL)) .comclose XSL
  .comopen XSL MSXML2.DOMDocument.6.0
  if ((!$com(XSL, async, 4, bool, false)) || ($comerr)) %error = Can't set async property on obj XSL
  if ((!$com(XSL, load, 1, bstr, %xslt)) || ($comerr)) %error = Can't load stylesheet information from ' $+ %xslt $+ '
 
  :error
  if ($error) %error = $v1 | reseterror
  if (%error) echo $color(info text) -ag %error
}
alias objWeather {
  var %error, %loc $url_encode($1-)
  var %api_url http://api.openweathermap.org/data/2.5/weather
  ; Please specify your own api key below
  var %api_key aa2e925237b320f371ec06fe2077c005
  ; You can specify units: `imperial`, `kelvin` and `metric`
  var %units metric
  var %page $+(%api_url,?q=,%loc,&units=,%units,&mode=xml,&appid=,%api_key)
 
  if ($com(XML)) .comclose XML
  .comopen XML MSXML2.DOMDocument.6.0 
  if ((!$com(XML, async, 4, bool, false)) || ($comerr)) %error = Can't set async property on obj XML
  if ((!$com(XML, load, 1, bstr, %page)) || ($comerr)) %error = Can't load XML information from ' $+ %page $+ '
 
  :error
  if ($error) %error = $v1 | reseterror
  if (%error) echo $color(info text) -ag %error
}
 
alias t1 {
  objStylesheet
  objWeather Vlissingen,nl
  if ($com(XML, transformNode, 1, dispatch, XSL)) {
    echo -ag Succeeded - $com(XML).result
  }
}

Last edited by kap; 26/08/20 04:35 PM. Reason: mention API key removal in few days

GNU Terry Pratchett
Re: XML -> XSLT -> plain text output;not working [Re: kap] #267647 31/08/20 01:56 AM
Joined: Feb 2015
Posts: 105
kap Offline OP
Vogon poet
OP Offline
Vogon poet
Joined: Feb 2015
Posts: 105
This seems to work for me:

Code
alias -l url_encode return $regsubex($1, /([\W\s])/Sg, $iif(\t == $chr(32), +, $+(%, $base($asc(\t), 10, 16, 2))))
alias objStylesheet {
  var %error, 
  bset -t &xslt 1 <?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="text" omit-xml-declaration="yes" /><xsl:template match="/">City: <xsl:value-of select="/current/city/@name" /> - Country: <xsl:value-of select="/current/city/country" /> - Temperature: <xsl:value-of select="/current/temperature/@value" /><xsl:value-of select="/current/temperature/@unit" /></xsl:template></xsl:stylesheet>
  if ($com(XSL)) .comclose XSL
  .comopen XSL MSXML2.DOMDocument.6.0
  if ((!$com(XSL, async, 4, bool, false)) || ($comerr)) %error = Can't set async property on obj XSL
  elseif ((!$com(XSL, loadXML, 1, &bstr, &xslt)) || ($comerr)) %error = Can't load stylesheet information

  :error
  if ($error) %error = $v1 | reseterror
  if (%error) echo $color(info text) -ag %error
}
alias objWeather {
  var %error, %loc $url_encode($1-)
  var %api_url http://api.openweathermap.org/data/2.5/weather
  ; Please specify your own api key below
  var %api_key aa2e925237b320f371ec06fe2077c005
  ; You can specify units: `imperial`, `kelvin` and `metric`
  var %units metric
  var %page $+(%api_url,?q=,%loc,&units=,%units,&mode=xml,&appid=,%api_key)

  if ($com(XML)) .comclose XML
  .comopen XML MSXML2.DOMDocument.6.0 
  if ((!$com(XML, async, 4, bool, false)) || ($comerr)) %error = Can't set async property on obj XML
  elseif ((!$com(XML, load, 1, bstr, %page)) || ($comerr)) %error = Can't load XML information from ' $+ %page $+ '

  :error
  if ($error) %error = $v1 | reseterror
  if (%error) echo $color(info text) -ag %error
}

alias t1 {
  objStylesheet
  objWeather $1-
  if ($com(XML, transformNode, 1, dispatch, XSL)) {
    echo -ag Succeeded - $com(XML).result
  }
}


execute

Code
/t1 Vlissingen,nl


and get:
[Linked Image from i.imgur.com]


GNU Terry Pratchett