xml - XSL for-each with position to get dynamic column-number -
this not working me. trying correct number of additional columns table depending upon number of nodes in xml. in example, have 2 "stuff" nodes, xml data can have between 1 , four, , table can have between 1 , 4 additional columns. first column there.
here's xml:
<stuff> <thing>house</thing> <color>red</color> </stuff> <stuff> <thing>hat</thing> <color>brown</color> </stuff>
here's xsl:
<fo:table width="100%" table-layout="fixed"> <fo:table-column column-width="27%" column-number="1"/> <xsl:for-each select="stuff"> <xsl:if test="position()=1"> <fo:table-column column-width="73%" column-number="2"/> </xsl:if> <xsl:if test="position()=2"> <fo:table-column column-width="36.5%" column-number="2"/> <fo:table-column column-width="36.5%" column-number="3"/> </xsl:if> <xsl:if test="position()=3"> <fo:table-column column-width="24%" column-number="2"/> <fo:table-column column-width="24%" column-number="3"/> <fo:table-column column-width="25%" column-number="4"/> </xsl:if> <xsl:if test="position()=4"> <fo:table-column column-width="18.25%" column-number="2"/> <fo:table-column column-width="18.25%" column-number="3"/> <fo:table-column column-width="18.25%" column-number="4"/> <fo:table-column column-width="18.25%" column-number="5"/> </xsl:if> </xsl:for-each> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block> <fo:block>first row</fo:block> </fo:block> </fo:table-cell> <xsl:for-each select="stuff"> <fo:table-cell> <fo:block> <xsl:value-of select="thing"/> </fo:block> </fo:table-cell> </xsl:for-each> </fo:table-row> <fo:table-row> <fo:table-cell> <fo:block> <fo:block>second row</fo:block> </fo:block> </fo:table-cell> <xsl:for-each select="stuff"> <fo:table-cell> <fo:block> <xsl:value-of select="color"/> </fo:block> </fo:table-cell> </xsl:for-each> </fo:table-row> </fo:table-body> </fo:table>
i think want this:
<xsl:template match="parent-of-stuff"> <fo:table width="100%" table-layout="fixed"> <fo:table-column column-width="27%" column-number="1"/> <xsl:variable name="n" select="count(stuff)" /> <xsl:for-each select="stuff"> <fo:table-column column-width="{73 div $n}%" column-number="{position() + 1}"/> </xsl:for-each> <!-- rest of table --> </fo:table> </xsl:template>
if result of column-width="24.3333333333333%"
3-column scenario not acceptable, do:
<xsl:template match="parent-of-stuff"> <fo:table width="100%" table-layout="fixed"> <fo:table-column column-width="27%" column-number="1"/> <xsl:variable name="n" select="count(stuff)" /> <xsl:for-each select="stuff"> <fo:table-column column-number="{position() + 1}"> <xsl:attribute name="column-width"> <xsl:choose> <xsl:when test="$n=3 , position()=3">25%</xsl:when> <xsl:when test="$n=3">24%</xsl:when> <xsl:otherwise> <xsl:value-of select="73 div $n" /> </xsl:otherwise> </xsl:choose> </xsl:attribute> </fo:table-column> </xsl:for-each> <!-- rest --> </fo:table> </xsl:template>
Comments
Post a Comment