I'm trying to import XML data via XSLT into Adobe InDesign. InDesign doesnt seem to be able to use attributes, even though it can see the attributes.
I have access to free weather data in this format : http://www.yr.no/sted/Norge/Oslo/Oslo/Oslo/varsel.xml
I'm not sure if the way I do the XSLT to transform attributes to regular XML format is the best, but it works.
The problem I'm having is that I need the weather data for tomorrow, but I only get the data for today. The data is stored in the weatherdata/forecast/tabular/time.
I use <xsl:param name="speedmps" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@mps" />
to select the data, but this only chooses the first match.
And the first match is todays weather (from 12:00 to 18:00), what I want is the second match (the second period='2' match, that is tomorrow 12:00 to 18:00).
The day is presented in 4 periods, and the XML file got the forecast for several days. Ideally I would want to make one XSLT for todays weather, one for tomorrows weather and so on.
My code is mostly cut and paste, and then I try to edit it to do what I want it to. I tried making a if block, but I failed to find any examples I could build onto. My skills in XSLT are too basic at the moment, and I have a short deadline. And what I discovered in the source data is that the periods change according to when in the day you fetch the data. So I couldnt say that it should select the 6th block, since tomorrows period='2' may be the 5th at this time of the day. (period='0' is 00:00 to 09:00 so period 0 gets deleted after 09:00 and so on).
The first match right now is:
<time from="2013-02-27T12:00:00" to="2013-02-27T18:00:00" period="2">
<!-- Valid from 2013-02-27T12:00:00 to 2013-02-27T18:00:00 -->
<symbol number="1" name="Klarvær" var="01d" />
<precipitation value="0" />
<!-- Valid at 2013-02-27T12:00:00 -->
<windDirection deg="187.2" code="S" name="Sør" />
<windSpeed mps="1.5" name="Flau vind" />
<temperature unit="celsius" value="-1" />
<pressure unit="hPa" value="1030.2" />
</time>
The second match right now is:
<time from="2013-02-28T12:00:00" to="2013-02-28T18:00:00" period="2">
<!-- Valid from 2013-02-28T12:00:00 to 2013-02-28T18:00:00 -->
<symbol number="1" name="Klarvær" var="01d" />
<precipitation value="0" />
<!-- Valid at 2013-02-28T12:00:00 -->
<windDirection deg="181.3" code="S" name="Sør" />
<windSpeed mps="3.1" name="Svak vind" />
<temperature unit="celsius" value="2" />
<pressure unit="hPa" value="1015.7" />
</time>
I'm sorry but english is not my first language, I tried to explain as good as I can.
This is my XSLT code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="ISO-8859-1" indent="yes" omit-xml-declaration="no" cdata-section-elements="" />
<!-- params -->
<xsl:param name="location" select="weatherdata/location/name"/>
<xsl:param name="alternate" select="weatherdata/links/link[@id='overview']/@url"/>
<xsl:param name="base" select="'file:///d:/temp/yr/sky/'"/>
<!-- We use the text forecast dates or? -->
<xsl:param name="lastupdate" select="weatherdata/meta/lastupdate" />
<xsl:param name="symbolnumber" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@number" />
<xsl:param name="symbolname" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@name" />
<xsl:param name="symbolvar" select="weatherdata/forecast/tabular/time[@period='2']/symbol/@var" />
<xsl:param name="precip" select="weatherdata/forecast/tabular/time[@period='2']/precipitation/@value" />
<xsl:param name="directiondeg" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@deg" />
<xsl:param name="directioncode" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@code" />
<xsl:param name="directionname" select="weatherdata/forecast/tabular/time[@period='2']/windDirection/@name" />
<xsl:param name="speedmps" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@mps" />
<xsl:param name="speedname" select="weatherdata/forecast/tabular/time[@period='2']/windSpeed/@name" />
<xsl:param name="tempvalue" select="weatherdata/forecast/tabular/time[@period='2']/temperature/@value" />
<xsl:template match="/">
<yr>
<testtag>
<title><xsl:value-of select="$location"/></title>
<alternate><xsl:value-of select="$alternate"/></alternate>
<base><xsl:value-of select="$base"/></base>
<lastupdate><xsl:value-of select="$lastupdate"/></lastupdate>
<symbolvar><xsl:value-of select="$symbolvar"/></symbolvar>
<symbolnr><xsl:value-of select="$symbolnumber"/></symbolnr>
<symbolname><xsl:value-of select="$symbolname"/></symbolname>
<precip><xsl:value-of select="$precip"/></precip>
<tempval><xsl:value-of select="$tempvalue"/></tempval>
<speedname><xsl:value-of select="$speedname"/></speedname>
<speedmps><xsl:value-of select="$speedmps"/></speedmps>
<dirdeg><xsl:value-of select="$directiondeg"/></dirdeg>
<dircode><xsl:value-of select="$directioncode"/></dircode>
<dirname><xsl:value-of select="$directionname"/></dirname>
<Image><xsl:attribute name="href"><xsl:value-of select="$base"/><xsl:value-of select="$symbolvar"/>.png</xsl:attribute></Image>
</testtag>
</yr>
</xsl:template>
</xsl:stylesheet>