Re: [xsl] table creation with element repetition

Subject: Re: [xsl] table creation with element repetition
From: David Carlisle <davidc@xxxxxxxxx>
Date: Fri, 30 Apr 2004 15:45:55 +0100
  Any attempt to create a proper template has encountered some xsl
  limitation :
  impossibility to open and close an element in two separate templates
  impossibility to change variable values at runtime.

Neither of those things is an XSLT limitation.

The first is a basic rule of XML, nothing specific about XSLT, element
structure has to be well formed, ie properly nested.
The second is just a feature of declarative programming languages, but
these are computationaly equivalent to imperative programming languages,
so this is not, itself, a limitation.

some comments on your code,


 <xsl:param name="iposition">0</xsl:param>

don't do that, this creates a result tree fragment with aroot node with
text node child with string value "0". What you want is teh number 0

 <xsl:param name="iposition" select="0"/>

these are not always equivalent, if iposition is a number then for
example you can go 

product[$iposition]

as a shorthand for

product[position()=$iposition]

but if $iposition is a result tree fragment then 

product[$iposition]

is short for

product[boolean($iposition)]

which is always


product[true()]

as a result tree fragment never corresponds to an empty node set, ie it
will always retuurn the whole node set


product

Note that the construct

 select="//product[$iposition]/

repeatedly searches the entire document to find that node, you should
not pass the node number as a parameter, but rather the node itself,
then you don't have to keep searching for it again.


<doc name="products print" >
  <products_to_print>
    <product copies="1">
      <description>product 1</description>
    </product>
    <product copies="2">
      <description>product 2</description>
    </product>
    <product copies="2">
      <description>product 3</description>
    </product>
  </products_to_print>
</doc>







<xsl:stylesheet 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:fo="http://www.w3.org/1999/XSL/Format";
 version="1.0">

<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="products_to_print">
<fo:table-body>
<xsl:apply-templates select="product[1]"/>
</fo:table-body>
</xsl:template>

<xsl:template match="product">
<xsl:param name="x" select="/.."/>
<xsl:variable name="y">
<fo:table-cell><xsl:apply-templates/></fo:table-cell>
</xsl:variable>
<xsl:variable name="c" select="@copies"/>
<xsl:choose>
<xsl:when test="not($x)">
  <xsl:for-each select="(//*)[position() &lt;= floor($c div 2)]">
  <fo:table-row>
   <xsl:copy-of select="$y"/>
   <xsl:copy-of select="$y"/>
  </fo:table-row>
  </xsl:for-each>
</xsl:when>
<xsl:otherwise>
  <fo:table-row>
   <xsl:copy-of select="$x"/>
   <xsl:copy-of select="$y"/>
  </fo:table-row>
  <xsl:for-each select="(//*)[position() &lt;= floor(($c - 1) div 2)]">
  <fo:table-row>
   <xsl:copy-of select="$y"/>
   <xsl:copy-of select="$y"/>
  </fo:table-row>
  </xsl:for-each>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="(not($x) and @copies mod 2 = 1) or ($x and @copies mod 2 = 0)">
  <xsl:apply-templates select="following-sibling::product[1]">
    <xsl:with-param name="x" select="$y"/>
  </xsl:apply-templates>
  <xsl:if test="not(following-sibling::product)">
  <fo:table-row>
   <xsl:copy-of select="$y"/>
   <fo:table-cell/>
  </fo:table-row>
  </xsl:if>
</xsl:when>
<xsl:otherwise>
  <xsl:apply-templates select="following-sibling::product[1]"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="description">
<fo:block><xsl:apply-templates/></fo:block>
</xsl:template>

</xsl:stylesheet>



$ saxon rep.xml repet.xsl
<?xml version="1.0" encoding="utf-8"?>
<fo:table-body xmlns:fo="http://www.w3.org/1999/XSL/Format";>
   <fo:table-row>
      <fo:table-cell>
         <fo:block>product 1</fo:block>
      </fo:table-cell>
      <fo:table-cell>
         <fo:block>product 2</fo:block>
      </fo:table-cell>
   </fo:table-row>
   <fo:table-row>
      <fo:table-cell>
         <fo:block>product 2</fo:block>
      </fo:table-cell>
      <fo:table-cell>
         <fo:block>product 3</fo:block>
      </fo:table-cell>
   </fo:table-row>
   <fo:table-row>
      <fo:table-cell>
         <fo:block>product 3</fo:block>
      </fo:table-cell>
      <fo:table-cell/>
   </fo:table-row>
</fo:table-body>




-- 
The LaTeX Companion
  http://www.awprofessional.com/bookstore/product.asp?isbn=0201362996
  http://www.amazon.co.uk/exec/obidos/tg/detail/-/0201362996/202-7257897-0619804




________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread