RE: [xsl] summation of attributes

Subject: RE: [xsl] summation of attributes
From: Jarno.Elovirta@xxxxxxxxx
Date: Tue, 27 May 2003 12:20:17 +0300
Hi,
 
> I am having a fop document that has to be transferred to 
> html. Right now I am struugling with the following situation. 
>  
> fop.xml : 
>  
> *snip* 
> <fo:table width="100%"> 
>  
> 	<fo:table-column column-width="proportional-column-width(68)"/> 
> 	<fo:table-column column-width="proportional-column-width(95)"/> 
>  
> 	.... continued .... 
>  
> </fo:table> 
> *snip> 
>  
> this has to be transferred to 
>  
> <table> 
> 	<colgroup> 
> 		<col width="41%"/>     <!-- 68 * 100 / (68 + 95) --> 
> 		<col width="59%"/>     <!-- 95 * 100 / (68 + 95) --> 
> 	</colgroup> 
>  
>     .. continued ... 
> </table> 

Erm... I suppose you have a good reason for it, but why on earth are you transforming FO to HTML?
  
> right know I am using the following templates 
>  
> <xsl:template match="fo:table"> 
> 	<table> 
> 		<xsl:variable name="summated_attributes" 
> select="sum(fo:table-column/ @column-width)"/> 
> 		<colgroup> 
> 			<xsl:apply-templates match="fo:table-column"> 

xsl:apply-templates doesn't have a match attribute--you want select.

[snip]

> 		<xsl:attribute name="width" 
> select="@column-width * 100 div $total_sum">%</xsl:attribute> 

xsl:attribute doesn't have select attribute. Use containing xsl:value-of.

> unfortunatly column-width is encoded as 
> "proportinal-column-with(68)" where 68 is the value I need 
> therefore I am also doing 
>  
> <xsl:template match="@column-width"> 
> 	<xsl:value-of select="substring(., 27,2)"/> 
> </xsl:template> 

Which would give you the valua, but the above 

  @column-width * 100 div $total_sum

Expression will not apply the template for @column-witdth, but rather just first retrieves the string value of @column-witdth and then casts that to a number. You need to call xsl:apply-templates explicitly inside an xsl:variable to use the template.
  
> _but this is not working_ 
>  
> does anybody have an idea ? 

For example

  <xsl:template match="fo:table">
    <table>
      <colgroup>
        <xsl:apply-templates select="fo:table-column">
          <xsl:with-param name="total_sum">
            <xsl:call-template name="sum">
              <xsl:with-param name="node-set" select="fo:table-column/@column-width"/>
            </xsl:call-template>
          </xsl:with-param>
        </xsl:apply-templates>
      </colgroup>
    </table>
  </xsl:template>
  <xsl:template match="fo:table-column">
    <xsl:param name="total_sum"/>
    <xsl:variable name="width">
      <xsl:apply-templates select="@column-width"/>
    </xsl:variable>
    <col width="{round($width * 100 div $total_sum)}%"/>
  </xsl:template>
  <xsl:template match="@column-width">
    <xsl:value-of select="substring(., 27,2)"/>
  </xsl:template>
  <!--recursive template to calculate the sum of @column-width values-->
  <xsl:template name="sum">
    <xsl:param name="node-set" select="/.."/>
    <xsl:param name="sum" select="0"/>
    <xsl:choose>
      <xsl:when test="$node-set">
        <xsl:variable name="width">
          <xsl:apply-templates select="$node-set[1]"/>
        </xsl:variable>
        <xsl:call-template name="sum">
          <xsl:with-param name="node-set" select="$node-set[not(position() = 1)]"/>
          <xsl:with-param name="sum" select="$sum + $width"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$sum"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

Cheers,

Jarno - SITD: Mortal (RMX by Solitary Experiments)

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread