Output 4 days in weatherdata xml using xslt -
i'm trying import xml data via xslt adobe indesign. want information next 4 days (not today). far i've been able tomorrows forecast not sure how output 4 days. template selects day 2 (tomorrow) , time period 2 (time of day)
i have access free weather data in format: http://www.yr.no/place/iceland/capital_region/reykjavik/forecast.xml
this template:
<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0"> <xsl:output method="xml" encoding="iso-8859-1" indent="yes" omit-xml-declaration="no" cdata-section-elements=""/> <xsl:strip-space elements="*"/> <!-- position of time[@period = '2'] element; indexing starts 1. can pass in parameter transformation if want time[@period = '1'] element in other position. --> <xsl:param name="time" select="2"/> <!-- day --> <xsl:param name="base" select="'file:///volumes/media/geymsla/ymis_verkefni/dv2013/sky/'"/> <xsl:template match="/"> <yr> <testtag> <xsl:apply-templates select="weatherdata"/> </testtag> </yr> </xsl:template> <xsl:template match="weatherdata"> <xsl:apply-templates select="location/name"/> <xsl:apply-templates select="links/link[@id = 'overview']"/> <base><xsl:value-of select="$base"/></base> <xsl:apply-templates select="meta/lastupdate"/> <!-- apply <time period="2"> element in $time position, time of day --> <xsl:apply-templates select="forecast/tabular/time[@period = '2'][number($time)]"/> </xsl:template> <xsl:template match="tabular/time"> <xsl:apply-templates select="symbol"/> <xsl:apply-templates select="precipitation"/> <xsl:apply-templates select="temperature"/> <xsl:apply-templates select="windspeed"/> <xsl:apply-templates select="winddirection"/> <!-- use attribute value template (avt) construct attribute value: http://lenzconsulting.com/how-xslt-works/#attribute_value_templates --> <image href="{concat($base, symbol/@var, '.png')}"/> </xsl:template> <xsl:template match="location/name"> <title> <xsl:value-of select="."/> </title> </xsl:template> <xsl:template match="links/link"> <alternate> <!-- use value of @url attribute of element --> <xsl:value-of select="@url"/> </alternate> </xsl:template> <xsl:template match="temperature"> <tempval> <xsl:value-of select="@value"/> </tempval> </xsl:template> <xsl:template match="windspeed"> <speedname> <xsl:value-of select="@name"/> </speedname> <speedmps> <xsl:value-of select="@mps"/> </speedmps> </xsl:template> <xsl:template match="winddirection"> <dirdeg> <xsl:value-of select="@deg"/> </dirdeg> <dircode> <xsl:value-of select="@code"/> </dircode> <dirname> <xsl:value-of select="@name"/> </dirname> </xsl:template> <xsl:template match="precipitation"> <precip> <xsl:value-of select="@value"/> </precip> </xsl:template> <xsl:template match="symbol"> <symbolvar> <xsl:value-of select="@var"/> </symbolvar> <symbolnr> <xsl:value-of select="@number"/> </symbolnr> <symbolname> <xsl:value-of select="@name"/> </symbolname> </xsl:template> <xsl:template match="lastupdate"> <!-- copy original node --> <xsl:copy> <xsl:apply-templates/> </xsl:copy> </xsl:template> </xsl:stylesheet>
you need adjust predicate filter. selecting of time
elements who's @period=2
, , sub-selecting matched elements second one:
<!-- apply <time period="2"> element in $time position, time of day --> <xsl:apply-templates select="forecast/tabular/time[@period = '2'][number($time)]"/>
in order select @period
specified $time
parameter use:
<xsl:apply-templates select="forecast/tabular/time[@period = number($time)]"/>
then can filer next 4 days applying predicate fitler on set:
<xsl:apply-templates select="forecast/tabular/time[@period = number($time)] [position() > 1 , position() <= 5]"/>
Comments
Post a Comment