Skip to content Skip to sidebar Skip to footer

Xslt Remove Table Column When All Specific Value

Using XSLT, I need to remove a complete table column (header + body cells) when a column contains only '0.00' or '-'. i.e. If all the values in the cells of one or more columns are

Solution 1:

I have assumed that you mean't to say if all the data cells of the column are 0.00/- then remove it, not just one of them. If I have misunderstood, please advise and I will update the solution style-sheets accordingly.

There are a lot of different ways and options to create tables, and so your solution will need to be adjusted to the type and structure of your table. Shown here is a solution for a simple form of table.

XSLT 1.0 Solution

This XSLT 1.0 style-sheet...

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template name="ident" match="@*|node()">
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="td">
  <xsl:variable name="col" select="count(preceding-sibling::td)+1" />
  <xsl:if test="../../tr/td[$col][. != '0.00'][. != '-']">
    <xsl:call-template name="ident" />
  </xsl:if>  
 </xsl:template>

</xsl:stylesheet>

...when applied to this input document...

<table>
  <th>
    <td>Contestant</td><td>Score</td><td>Country</td>
  </th> 
  <tr>
    <td>Jack</td><td>0.00</td><td>AUS</td>
  </tr> 
  <tr>
    <td>Jill</td><td>-</td><td>-</td>
  </tr> 
</table> 

...will yield...

<table>
  <th>
    <td>Contestant</td>
    <td>Country</td>
  </th>
  <tr>
    <td>Jack</td>
    <td>AUS</td>
  </tr>
  <tr>
    <td>Jill</td>
    <td>-</td>
  </tr>
</table>

Only one column is removed - the one with all "empty" non-header cells.

XSLT 2.0 Solution

And here is the XSLT 2.0 equivalent...

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
 <xsl:copy>
   <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
</xsl:template>

<xsl:template match="td[ not(
  for $c in count(preceding-sibling::td)+1 return
    ../../tr/td[$c][.!='0.00'][.!= '-']  )]" />

</xsl:stylesheet>

Solution 2:

I. A similar but more efficient (O(N)) XSLT 1.0 solution (Sean's XSLT 1.0 solution is O(N^2), where N is the number of columns):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vrtfColProps">
  <xsl:for-each select="/*/tr[1]/td">
    <xsl:variable name="vPos" select="position()"/>
    <col pos="{$vPos}">
          <xsl:if test="/*/tr/td[$vPos][not(. = 0.00 or . ='-')]">
           1
          </xsl:if>
      </col>
      </xsl:for-each>
 </xsl:variable>

 <xsl:variable name="vColProps" select="ext:node-set($vrtfColProps)/*"/>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match="td">
  <xsl:variable name="vPos" select="position()"/>
  <xsl:if test="$vColProps[@pos=$vPos]/node()">
   <xsl:call-template name="identity"/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

when this transformation is applied on the following document:

<table border="1">
  <th>
   <tr>
    <td>Contestant</td><td>Score</td><td>Country</td>
   </tr>
  </th>
  <tr>
    <td>Jack</td><td>0.00</td><td>AUS</td>
  </tr>
  <tr>
    <td>Jill</td><td>-</td><td>-</td>
  </tr>
</table>

the wanted, correct result is produced:

<table border="1">
   <th>
      <tr>
         <td>Contestant</td>
         <td>Country</td>
      </tr>
   </th>
   <tr>
      <td>Jack</td>
      <td>AUS</td>
   </tr>
   <tr>
      <td>Jill</td>
      <td>-</td>
   </tr>
</table>

II. More efficient (linear vs Sean's quadratical complexity) XSLT 2.0 solution:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:variable name="vColProps">
  <xsl:for-each select="/*/tr[1]/td">
    <xsl:variable name="vPos" select="position()"/>
    <col pos="{$vPos}">
          <xsl:if test="/*/tr/td[$vPos][not(. = '0.00' or . = '-')]">
           1
          </xsl:if>
      </col>
      </xsl:for-each>
 </xsl:variable>

 <xsl:template match="node()|@*" name="identity">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>

 <xsl:template match=
 "td[for $vPos in position()
      return
         not($vColProps/*[@pos=$vPos]/node())
    ]"/>
</xsl:stylesheet>

When this transformation is applied on the same XML document (above), the same wanted, correct result is produced:

<table border="1">
   <th>
      <tr>
         <td>Contestant</td>
         <td>Country</td>
      </tr>
   </th>
   <tr>
      <td>Jack</td>
      <td>AUS</td>
   </tr>
   <tr>
      <td>Jill</td>
      <td>-</td>
   </tr>
</table>

Post a Comment for "Xslt Remove Table Column When All Specific Value"