views:

42

answers:

1

I'm generating a table in a pdf document using xml-fo.
The number of columns is dynamic. Columns are only displayed when tests are passing.
In tests I'm using variables like $var1, $var2, ... that point to xml nodes.

I've something like

 <xsl:element name="fo:table-row">
    <xsl:element name="fo:table-cell" use-attribute-sets="table-cell">
        <xsl:attribute name="number-columns-spanned"> **???** </xsl:element>
        <xsl:element name="fo:block">Text that spans over columns</xsl:element>
    </xsl:element>
 </xsl:element>
 <xsl:element name="fo:table-row" use-attribute-sets="headerRow"> 
    <xsl:element name="fo:table-cell" use-attribute-sets="table-cell">
       <xsl:element name="fo:block">some value here</xsl:element>
    </xsl:element>
    <xsl:if test="$var1">
       <xsl:element name="fo:table-cell" use-attribute-sets="table-cell">
          <xsl:element name="fo:block">maybe some value here 2</xsl:element>
       </xsl:element>
    </xsl:if>
    <xsl:if test="$var2">
       <xsl:element name="fo:table-cell" use-attribute-sets="table-cell">
          <xsl:element name="fo:block">maybe some value here 3</xsl:element>
       </xsl:element>
    </xsl:if>
    ...
</xsl:element>

I'm expanding the 1st row to span all over the columns. "number-columns-spanned" is variable and depends on the number of test that are passing.
Is there an easy way to dynamically extract the number of conditions that are passing in order to correctly define the attribute number-columns-spanned?

+2  A: 

You can count the contents of the variables using an expression like this:

count($var1 | $var2 | $var3 | ...)

That's assuming each of your variables is a possibly-empty set. If your variables are true/false values instead then you can take advantage of the fact that false is 0 and true is 1 in XSL (at least with the interpreter I'm using):

$var1 + $var2 + $var3 + ...

If the values aren't node sets and aren't booleans either, convert them to booleans first:

boolean($var1) + boolean($var2) + boolean($var3) + ...

Not really a very elegant way of handling it but it works.

According to the XPath spec, booleans are indeed supposed to behave this way when cast to numerics. Good to know!

Welbog