tags:

views:

40

answers:

2

I found an elegant solution for that problem here: http://stackoverflow.com/questions/3806578

I'd like to understand the xslt code and I was wondering if you could help me to understand it by taking a look at the link provided above.

Basically there are 3 <xsl:template>. To me the first 2 ones are enough to achieve the purpose. However I tried with only 2 <xsl:template> and it doesn't work. In short the third one is required. Here it is:

<xsl:template match="gallery[not(position() mod 6 = 1)]"/>

The second template has a mode while the last one has not.

I have no idea when the last one is executed. Could you please help me to figure out it?

Thanks for your help.

Regards,

Roland

+1  A: 

The first template matches position 1 and 7, the second template is called from within the first template to output all the siblings. The last template matches position 2,3,4,5,6,8 and 9 so that noting happens to those positions again..like a do nothing-template.

Tommy
+3  A: 

Here is the complete code you were asking about. I happen to be the author, so let me explain:

<xsl:stylesheet version="1.0" 
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; 
 <xsl:output omit-xml-declaration="yes" indent="yes"/> 
 <xsl:strip-space elements="*"/> 

The following template overrides the XSLT built-in template for element nodes. It matches every 6k+1th gallery element. It cretes a tr element and inside its body puts the results of processing this gallery element togeether with the next 5. The processing is done in a special mode ("proc") to distinguish this from the default anonymous mode in which the XSLT built-in templates started and continue to operate.

 <xsl:template match="gallery[position() mod 6 = 1]"> 
  <tr> 
   <xsl:apply-templates mode="proc" 
        select=".|following-sibling::gallery[not(position() > 5)]" 
   /> 
  </tr> 
 </xsl:template> 

The following template is invoked in mode "proc" to process every gallery element in a group of 6 that should be in the same row.

<xsl:template match="gallery" mode="proc"> 
  <td> 
    <img src="{gallery-image-location}" alt="{gallery-image-alt}"/> 
  </td> 
 </xsl:template> 

The following template overrides the default processing of the XSLT built-in templates for all gallery elements, whose position is not of the type 6k+1 (they are not starting a new 6-tuple). It says simply not to do anything with any such element, because these elements are already processed in "proc" mode.

 <xsl:template match="gallery[not(position() mod 6 = 1)]"/> 
</xsl:stylesheet> 

You need to acquaint yourself with XSLT's processing model, default processing and built-in templates.

Dimitre Novatchev
+1 for explanation.
Alejandro
@Dimitri: Thanks Dimitri, I now understand how the code was built :D
roland
@Alejandro: I didn't know it was a mirror actually :p Shame on me I now ;) To be honest I didn't know what to do exactly to get an answer to my question: posting here, contacting Dimitri himself, letting a comment at the end of the original Dimitri's comment, ... Sorry if I polluted that forum but the explanation worths a new topic in my humble opinion ;)
roland