Re: How can I not include empty elements?

On Wed, May 14 2008 13:50:02 +0100, jean.stachler@crown.com wrote:
> I have an XML document that will display the elements as shown below.
> Basically, the elements are a subset of the <row> tag.  The elements
> will always have the tag names of column1, column2, column3, and column4.
> As shown in the example below, some of the tag names column1, etc. may
> have elements, and some of the tag names for column1, etc. may be empty
> elements.  I have only given one listing of empty elements, but this could
> be up to
> 50 empty elements.  When creating the PDF document in xslt, is there a way
> of  removing
> the empty elements so that they won't display?
> I have the xslt code after the xml detail.

YMMV, but you could try <fo:table-cell empty-cells="hide"> [1] which, if
implemented by your processor, should mean that empty cells don't have
any borders and entirely empty rows don't appear at all.

If you want to omit empty rows (and empty-cells="hide" doesn't work for
you), you could change:

>               <xsl:for-each select="header/row">    

to:

  <xsl:for-each select="header/row[normalize-space()]">

along the lines of the post by Mark Lundquist.

If you went from a "pull" style of selecting what to process to a "push"
style of the document structure determining which template rule is used
then you could go to something like:

------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  version="1.0">

  <xsl:output method="xml"/>

  <xsl:template match="/">
    <fo:block  text-align="left">
      <fo:table table-layout="fixed" >
        <fo:table-column column-width="15mm"/>
        <fo:table-column column-width="20mm"/>
        <fo:table-column column-width="14mm"/>
        <fo:table-column column-width="50mm"/>

        <fo:table-body>
          <xsl:apply-templates/>
        </fo:table-body>
      </fo:table>
    </fo:block>
  </xsl:template>

  <xsl:template match="row">
    <fo:table-row border-after-style="double" >
      <fo:table-cell  xsl:use-attribute-sets="cell-padding"
        border="solid black 0.5px" >
        <fo:block xsl:use-attribute-sets="detailtablecenter">
          <xsl:value-of select="column1"/>
        </fo:block>
      </fo:table-cell>

      <fo:table-cell  xsl:use-attribute-sets="cell-padding"
        border="solid black 0.5px" >
        <fo:block xsl:use-attribute-sets="detailtablecenter">
          <xsl:value-of select="column2"/>
        </fo:block>
      </fo:table-cell>

      <fo:table-cell xsl:use-attribute-sets="cell-padding"
        border="solid black 0.5px" >
        <fo:block xsl:use-attribute-sets="detailtablecenter">
          <xsl:value-of select="column3"/>
        </fo:block>
      </fo:table-cell>

      <fo:table-cell xsl:use-attribute-sets="cell-padding"
        border="solid black 0.5px" >
        <fo:block xsl:use-attribute-sets="detailtableleft">
          <xsl:value-of select="column4"/>
        </fo:block>
      </fo:table-cell>
    </fo:table-row>
  </xsl:template>

  <xsl:template match="row[not(normalize-space())]"/>

</xsl:stylesheet>
----------------------------------------------------------------------

As always, there's many ways to slice and dice the problem.  One of
several ways to also reduce the repetition of the fo:table-cell elements
is using a lookup for attribute values that can differ, e.g.:

------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fo="http://www.w3.org/1999/XSL/Format"
  xmlns:crown="http://crown.com"
  version="1.0">

  <xsl:output method="xml"/>

  <crown:align name="column1">center</crown:align>
  <crown:align name="column2">center</crown:align>
  <crown:align name="column3">center</crown:align>
  <crown:align name="column4">left</crown:align>

  <xsl:attribute-set name="detailtablealign">
    <xsl:attribute name="text-align">
      <xsl:value-of
        select="document('')/*/crown:align[@name=local-name(current())]"/>
      <!-- Define other common attributes here. -->
    </xsl:attribute>
  </xsl:attribute-set>

  <xsl:template match="/">
    <fo:block  text-align="left">
      <fo:table table-layout="fixed" >
        <fo:table-column column-width="15mm"/>
        <fo:table-column column-width="20mm"/>
        <fo:table-column column-width="14mm"/>
        <fo:table-column column-width="50mm"/>

        <fo:table-body>
          <xsl:apply-templates/>
        </fo:table-body>
      </fo:table>
    </fo:block>
  </xsl:template>

  <xsl:template match="row">
      <fo:table-row border-after-style="double" >
        <xsl:apply-templates/>
      </fo:table-row>
  </xsl:template>

  <xsl:template match="row[not(normalize-space())]"/>

  <xsl:template match="column1 | column2 | column3 | column4">
    <fo:table-cell xsl:use-attribute-sets="cell-padding"
      border="solid black 0.5px" >
      <fo:block xsl:use-attribute-sets="detailtablealign">
        <xsl:apply-templates/>
      </fo:block>
    </fo:table-cell>
  </xsl:template>
</xsl:stylesheet>
------------------------------------------------------------

Regards,


Tony Graham                         Tony.Graham@MenteithConsulting.com
Director                                  W3C XSL FO SG Invited Expert
Menteith Consulting Ltd
XML, XSL and XSLT consulting, programming and training
Registered Office: 13 Kelly's Bay Beach, Skerries, Co. Dublin, Ireland
Registered in Ireland - No. 428599   http://www.menteithconsulting.com
  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --  --
xmlroff XSL Formatter                               http://xmlroff.org
xslide Emacs mode                  http://www.menteith.com/wiki/xslide
Unicode: A Primer                               urn:isbn:0-7645-4625-2


[1] http://www.w3.org/TR/xsl11/#empty-cells

Received on Wednesday, 14 May 2008 14:49:23 UTC