Output elements dynamically based on condition in XSL?

treax

New Member
Assume the following xml input...\[code\]<incidents> <incident> <year>2011</year> <other data here> </incident> <incident> <year>2009</year> <other data here> </incident> <incident> <year>2006</year> </incident></incidents>\[/code\]The xml is always presorted by year so the latest incident year is first. I need to process it using xsl and basically output the data with minimal transform for a 5 years back, max, but if any years are missing, I just need to output an element for \[code\]<incident><year>missingYear</year></incident>\[/code\].So, assuming I had the correct XSL to do this, processing the above xml would produce this...\[code\]<incidents> <incident> <year>2011</year> </incident> <incident> <year>2010</year> </incident> <incident> <year>2009</year> </incident> <incident> <year>2008</year> </incident> <incident> <year>2007</year> </incident> <incident> <year>2006</year> </incident></incidents>\[/code\]I got this far with the xsl, but it's not accounting for large gaps between in years\[code\]<xsl:variable name="maxYear" select="/incidents/incident/year[1]"></xsl:variable><xsl:template match="incidents" > <xsl:element name="incident"> <xsl:for-each select="incident"> <xsl:variable name="currentYear" select="year"/> <xsl:choose> <xsl:when test="($maxYear - (position() -1)) != $currentYear"> <!-- output the missing year --> <xsl:element name="year"> <xsl:value-of select="($maxYear - (position() -1))" /></xsl:element> <!-- output the current year node --> <xsl:element name="year"> <xsl:value-of select="$currentYear" /></xsl:element> </xsl:when> <xsl:eek:therwise> <xsl:element name="year"> <xsl:value-of select="$currentYear" /></xsl:element> </xsl:eek:therwise> </xsl:choose> </xsl:for-each> </xsl:element></xsl:template>\[/code\]
 
Top