XSL-FO- Specifying conditional visibility

P.Brian.Mackey picture P.Brian.Mackey · Sep 27, 2011 · Viewed 8.2k times · Source

I have an HTML table that varies according to its content. 1-3 columns hide. 1 column name changes. All depending on content.

I am creating a PDF version of this HTML table. The PDF version uses apache-FOP with fop v1.0. The PDF output contains 2 of the aforementioned tables on one page. I do not want to create an .xsl for every combination of possibilities. That's a lot of duplication and maintenance.

I can solve the column name change simply by passing the column name in with the XML content. But, conditional visibility of the columns seems to be a far more challenging task.

How can I setup conditional visibility? Is it possible?


I'm having difficulty just getting <fo:table-column visibility="collapse" /> to work. The data still displays with visility set to hidden or collapse. display="none" looked promising. But, the API doesn't show it as a valid property for a table-column.

If I can't conditionally hide a column then I'll need to produce 18 unique xsl files...


Currently, my tables are very basic. I do

<fo:block font-size="10pt">
  <fo:table table-layout="fixed" width="100%" border-collapse="separate">
    <fo:table-column />
    <fo:table-column />
    <fo:table-column />
    <fo:table-column />
    <fo:table-column />
    <fo:table-column visibility="collapse" />
    <fo:table-column />
    <fo:table-column />
    <fo:table-column />
    <fo:table-column />
    <fo:table-header>
      <fo:table-cell border-width="0.2mm" border-style="solid">
        <fo:block>Header 1</fo:block>
      </fo:table-cell>
      //...9 other headers (these should show/hide as needed)
    </fo:table-header>
    <fo:table-body>
      <xsl:for-each select="Object/type/SomeItem/ProcedureItemCollection">
        <fo:table-row>
          <fo:table-cell border-width="0.2mm" border-style="solid" >
            <fo:block>
              <xsl:value-of select="content1"/>
            </fo:block>
          </fo:table-cell>
          //...9 other cells...
        </fo:table-row>
      </xsl:for-each>
    </fo:table-body>
  </fo:table>
</fo:block>

XML

<Procedure>
  <SiteName>Site1</SiteName>//If Site1, don't create column 2
                            //If Site2, don't create column 2,3,4
                            //If Site3, create all columns
   <itemtype1>
            <item><member1></member1><member2></member2></item>
            <item><member1></member1><member2></member2></item>
   </itemtype1>
   <itemtype2>
            <item><member1></member1><member2></member2></item>
            <item><member1></member1><member2></member2></item>
   </itemtype2>
</Procedure>

Doing it this way, I have little flexibility in creating the table. But, this is all I know how to do.

Answer

P.Brian.Mackey picture P.Brian.Mackey · Sep 28, 2011

After a lot of tinkering it turns out that I can add/remove columns using xsl:when and a variable.

First create a variable

<xsl:variable name="SiteName" select="Procedure/SiteName" />

Then conditionally create the 3 elements of the table (column definition, header, body). Starting with the column definition...

          <xsl:choose>
            <xsl:when test="$SiteName = 'Site1'">
              <fo:table-column />//column 2
            </xsl:when>
          </xsl:choose>

Then the header

           <xsl:choose>
              <xsl:when test="$SiteName = 'Site1'">
                    <fo:table-cell border-width="0.2mm" border-style="solid">
                      <fo:block>Column2</fo:block>
                    </fo:table-cell>
              </xsl:when>
            </xsl:choose>

Finally, the body

                <xsl:choose>                                           
                        <xsl:when test="$SiteName = 'Site1'">
                          <fo:table-cell border-width="0.2mm" border-style="solid" >
                            <fo:block>
                              <xsl:value-of select="column2value"/>
                            </fo:block>
                          </fo:table-cell>
                        </xsl:when>                          
                </xsl:choose>