This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Calculating column widths


Hello Mike,

> > My question:
> > how can I do this calculation in one parse?
> > The sum() function requires a node-set
> > and numeric values to function properly,
> > and I have been unable to do the stripping
> > and the calculation in one template.
> 
> You could do the summation "by hand" in a recursive template, 
> rather than
> using the sum() function, but I'm not sure I'd recommend 
> this. It rather
> depends what you're trying to achieve.
> 
>
Yes, I had tried this, based on the example
on page 502 of your book (1st edition).
I would prefer it, but I can't get it
to work. I am enclosing the XSLT code. In this,
I am only stripping the proportional unit
indicator "*" from the CALS colwidth value. 
This stylesheet excerpt as it stands
is returning "Error" for the variable "rest"
and so the whole calculation won't work.
Also, the "self" variable is giving me
an empty value....
(I tried with Saxon 6.02 or Saxon 6.3)

<!-- Nested tables: no frame -->
<xsl:template match="entrytbl">
  <td><table>
    <xsl:attribute name="border">0</xsl:attribute>
    <xsl:attribute name="class">color</xsl:attribute>
    <xsl:if test="boolean(colspec[@colwidth]) or colspec[@align]">
      <colgroup>
        <xsl:call-template name="make.colgroup"/>
      </colgroup>
    </xsl:if>
    <xsl:apply-templates/>
  </table></td>
</xsl:template>

<xsl:template name="make.colgroup">
      <xsl:for-each select="colspec">
        <col>
          <!-- grab alignment -->
          <xsl:if test="@align">
            <xsl:attribute name="align">
              <xsl:value-of select="@align"/>
            </xsl:attribute>
          </xsl:if>
          <!-- read in value of column width -->
          <xsl:if test="@colwidth">
            <xsl:variable name="width">
              <xsl:call-template name="calculate.colwidth">
                <xsl:with-param name="context" select="../colspec"/>
              </xsl:call-template>
            </xsl:variable>
            <xsl:attribute name="width">
              <xsl:value-of select="concat($width,'%')"/>
            </xsl:attribute>
          </xsl:if>
        </col>
      </xsl:for-each>
</xsl:template>

<!-- the context of this named template will be colspec -->
  <xsl:template name="calculate.colwidth">
    <xsl:param name="context"/>
    <xsl:choose>
      <xsl:when test="$context">
        <xsl:variable name="first">
          <xsl:apply-templates select="$context[1]/@colwidth"/>
        </xsl:variable>
        <xsl:variable name="rest">
          <xsl:call-template name="calculate.colwidth">
            <xsl:with-param name="context"
select="$context[position()!=1]/@colwidth"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="sum">
         <xsl:value-of select="$first + $rest"/>
        </xsl:variable>
        <xsl:variable name="sum-as-number">
         <xsl:value-of select="substring-before($sum, '*')"/>
        </xsl:variable>
        <xsl:variable name="self">
          <xsl:variable name="widthval">
             <xsl:value-of select="@colwidth"/>
          </xsl:variable>
          <xsl:value-of select="substring-before($widthval, '*')"/>
        </xsl:variable>
        <xsl:value-of select="$rest"/>
        <!-- what I really want is: -->
        <!--xsl:value-of select="round(($self div $sum-as-number) * 97)"/-->
      </xsl:when>
      <xsl:otherwise>Error</xsl:otherwise>
    </xsl:choose>
  </xsl:template>

What am I doing wrong?

Thanks,
Marc


 

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]