XSLT transformation of weather.com data

Downloads section contains XSLT files for all three types of XML data sets returned by weather.com service.
  • LocationSearch.xsl - contains transformation for location search results
  • CurrentConditions.xsl - contains transformation for Current conditions
  • WeatherForecast.xsl - contains transformation for Weather forecast
  • Common.xsl - this file is shared between CurrentConditions.xsl and WeatherForecast.xsl

XSLT sample application

Sample application called XsltWeatherResultTransformation that can be found in the Downloads section has been developed to show how simple is to apply transformation to weather.com service XML data.
Code snippet below is an extract from the sample app showing how to transform weather conditions data in XML format to HTML file.


using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Weather.com.Client;
using Weather.com.Client.WeatherConditions.Location;

....

//Load XSLT transformation
string xsltPath = @".\XmlResponses/Xslt/CurrentConditions.xsl";
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = CredentialCache.DefaultCredentials;
XslCompiledTransform transformXmlToHtml = new XslCompiledTransform();
transformXmlToHtml.Load(new XPathDocument(xsltPath), XsltSettings.Default, resolver);
//Retrieve Weather.com data
WeatherClient weatherClient = new WeatherClient("Parner Id here", "License Key here");
List<Location> locations = weatherClient.GetLocation("Paris, France");
string xml = weatherClient.GetCurrentConditionsAsXml(locations[0]);
//Transform XML into HTML
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlReader reader = XmlReader.Create(new StringReader(xml));
using (XmlWriter textWriter = XmlWriter.Create("Current_Weather_Conditions_In_Paris.html", settings))
{
    transformXmlToHtml.Transform(reader, textWriter);
}

Last edited Jul 6, 2011 at 6:51 PM by mulawam, version 3

Comments

No comments yet.