Jump to content

xsl compare previous node


henny

Recommended Posts

hi there,I've an agenda.xml with mutilple entries per monthand I would like to create a separator when the month changes ( add month-name and horizontal line to the report )I use this code in my XSL ( I've left out the agenda formatting to concentrate on the separator issue )

  <!-- agenda header -->  <xsl:for-each select="agenda/activity[hidden = 0]">	<xsl:choose>	  <xsl:when test="position() = 1">		<tr><td width="100%" colspan="2"><xsl:value-of select="month"/></td></tr>		<tr><td width="100%" height="2" colspan="2" bgcolor="Navy"></td></tr>	  </xsl:when>	  <xsl:otherwise>		<xsl:if test="preceding::month[1] != month">		  <tr><td width="100%" colspan="2"><xsl:value-of select="month"/></td></tr>		  <tr><td width="100%" height="2" colspan="2" bgcolor="Navy"></td></tr>		</xsl:if>	  </xsl:otherwise>	</xsl:choose>  <!-- the agenda follows -->  </xsl:for-each>

this works fine, but only if I don't have a hidden item at the beginning of the monththat is very strange, because in my " <xsl:for-each select " I filter out all the hidden itemsand still they appear in the preceding::month[1] comparisonany clue to avoid this ??thx in advance - gr. Henny

Link to comment
Share on other sites

How about making the 0 a string. Like so:<xsl:for-each select="agenda/activity[hidden = '0']">

Link to comment
Share on other sites

Then maybe if you add additonal predicate in preceding itself for the hidden element:preceding::month[1][[hidden = 0]

Link to comment
Share on other sites

I'm going to have to take this to the lab... any sample XML you could give?

Link to comment
Share on other sites

here you go - I've stripped out some content to make the files smaller and sorry for the Dutch words ;-)in this example the month of September is not inserted as separatorbecause the 1st entry of september is an hidden entryXSL

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:template match="/"><html><body><table border="0" cellspacing="0" cellpadding="2" width="90%" align="center">	<tr>		<td width="30%" valign="top" class="agenda_header">Datum</td>		<td width="70%" valign="top" class="agenda_header">Omschrijving</td>	</tr>	<xsl:for-each select="agenda/activiteit[hidden = 0]">		<xsl:if test="(position() = 1) or (preceding::maand[1] != maand)">			<tr><td width="100%" colspan="2" valign="top" class="agenda_subheader"><xsl:value-of select="maand"/></td></tr>			<tr><td width="10%" height="2" colspan="2" bgcolor="Navy"></td></tr>		</xsl:if>		<tr>			<td width="30%" valign="top" class="agenda_record{position() mod 2}"><xsl:value-of select="datum"/></td>			<td width="70%" valign="top" align="left" class="agenda_record{position() mod 2}"><xsl:apply-templates select="omschrijving"/></td>		</tr>	</xsl:for-each></table></body></html></xsl:template><!-- 	enkele tekst formatting templates --><xsl:template match="br">   <br /></xsl:template><xsl:template match="b">	<span style="font-weight:bold"><xsl:value-of select="."/></span></xsl:template><xsl:template match="i">	<span style="font-style:italic"><xsl:value-of select="."/></span></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

It's a bit messy and unefficient solution, as the processor has to loop over hidden months and dates too, but it works:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output indent="yes"/><xsl:template match="/"><html><body><table border="0" cellspacing="0" cellpadding="2" width="90%" align="center">	<tr>		<td width="30%" valign="top" class="agenda_header">Datum</td>		<td width="70%" valign="top" class="agenda_header">Omschrijving</td>	</tr>	<xsl:for-each select="agenda/activiteit">		<xsl:if test="(position() = 1) or (preceding::maand[1] != maand)">			<tr><td width="100%" colspan="2" valign="top" class="agenda_subheader"><xsl:value-of select="maand"/></td></tr>			<tr><td width="10%" height="2" colspan="2" bgcolor="Navy"></td></tr>		</xsl:if>		<xsl:if test="hidden = 0">		<tr>			<td width="30%" valign="top" class="agenda_record{position() mod 2}"><xsl:value-of select="datum"/></td>			<td width="70%" valign="top" align="left" class="agenda_record{position() mod 2}"><xsl:apply-templates select="omschrijving"/></td>		</tr>		</xsl:if>	</xsl:for-each></table></body></html></xsl:template><!-- 	enkele tekst formatting templates --><xsl:template match="br">   <br /></xsl:template><xsl:template match="b">	<span style="font-weight:bold"><xsl:value-of select="."/></span></xsl:template><xsl:template match="i">	<span style="font-style:italic"><xsl:value-of select="."/></span></xsl:template></xsl:stylesheet>

[edit]Actually, here's a more efficient solution:

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output indent="yes"/><xsl:template match="/"><html><body><table border="0" cellspacing="0" cellpadding="2" width="90%" align="center">	<tr>		<td width="30%" valign="top" class="agenda_header">Datum</td>		<td width="70%" valign="top" class="agenda_header">Omschrijving</td>	</tr>	<xsl:for-each select="agenda/activiteit[hidden = 0]">		<xsl:variable name="curPos" select="position()"/>		<xsl:if test="(position() = 1) or (../activiteit[hidden = 0][position() < $curPos][position() = last()]/maand != maand)">			<tr><td width="100%" colspan="2" valign="top" class="agenda_subheader"><xsl:value-of select="maand"/></td></tr>			<tr><td width="10%" height="2" colspan="2" bgcolor="Navy"></td></tr>		</xsl:if>		<tr>			<td width="30%" valign="top" class="agenda_record{position() mod 2}"><xsl:value-of select="datum"/></td>			<td width="70%" valign="top" align="left" class="agenda_record{position() mod 2}"><xsl:apply-templates select="omschrijving"/></td>		</tr>	</xsl:for-each></table></body></html></xsl:template><!-- 	enkele tekst formatting templates --><xsl:template match="br">   <br /></xsl:template><xsl:template match="b">	<span style="font-weight:bold"><xsl:value-of select="."/></span></xsl:template><xsl:template match="i">	<span style="font-style:italic"><xsl:value-of select="."/></span></xsl:template></xsl:stylesheet>

btw, may I say this is one of the worst tables I've seen. You can achieve the same (if not even better) effect with lists and maybe, just maybe <hr />, though that's not exactly a best practice either.[/edit]

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...