views:

136

answers:

3

Hi,
How to delete an empty column in HTML table using XSLT, and having something like this:


 <table id="cas6">
    <tr>
      <td />
      <td>
        <table>
          <tr>
            <td>rechin</td>
            <td />
          </tr>
          <tr>
            <td>amarillo</td>
            <td />
          </tr>
        </table>
      </td>
    </tr>
  </table>
  <table id="cas7">
    <tr>
      <td>rechin</td>
      <td />
    </tr>
    <tr>
      <td>amarillo</td>
      <td />
    </tr>
    <tr>
      <td>this shouldn't been</td>
      <td>deleted</td>
    </tr>
  </table>

To delete the empty column, this being said to remove td's which are empty in all tr's in a Xth position

A: 

If my understanding of the question is correct, for a given table, if all entries in the nth column are empty, you want to delete that column from the table?

Try this XSLT then

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
   <xsl:template match="td">
      <xsl:variable name="columnNumber" select="position()"/>
      <xsl:if test="../../tr/td[position()=$columnNumber][* or text()]">
         <xsl:copy>
            <xsl:value-of select="$columnNumber"/>
            <xsl:apply-templates select="@*|node()"/>
         </xsl:copy>
      </xsl:if>
   </xsl:template>
   <xsl:template match="node()|@*">
      <xsl:copy>
         <xsl:apply-templates select="@*|node()"/>
      </xsl:copy>
   </xsl:template>
</xsl:stylesheet>

This is the 'identity' transform, but when it matches a TD element, it first gets the column number, and then checks if any other column in other rows are empty. If it finds any non-empty cells in the same column, it copies the TD element, otherwise it is ignored.

Tim C
<pre> <table id="cas7"> <tr> <td>1rechin</td> <td>2</td> </tr> <tr> <td>1amarillo</td> <td>2</td> </tr> <tr> <td>1this shouldn't been</td> <td>2deleted</td> </tr> </table></pre>
Monomachus
A: 

This is the XSLT that worked for me.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform&quot;&gt;
  <xsl:output omit-xml-declaration="yes" 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(node())]">
    <xsl:variable name="pos" select="position()" />
    <xsl:variable name="emptyTds" select="count(../../tr/td[position() = $pos and not(node())])" />
    <xsl:variable name="allTds" select="count(../../tr/td[position() = $pos])" />
    <xsl:if test="$emptyTds != $allTds">
      <xsl:copy>
        <xsl:value-of select="."/>
      </xsl:copy>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

Monomachus
+1  A: 

Here is a very simple solution:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

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

 <xsl:template match="td[not(node())]">
  <xsl:variable name="vPos" select="position()"/>

  <xsl:if test="../../tr/td[position() = $vPos]/node()">
    <xsl:copy-of select="."/>
  </xsl:if>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML document (made well-formed):

<html>
    <table border="1" id="cas6">
        <tr>
            <td/>
            <td>
                <table border="1">
                    <tr>
                        <td>rechin</td>
                        <td />
                    </tr>
                    <tr>
                        <td>amarillo</td>
                        <td />
                    </tr>
                </table></td>
        </tr>
    </table>
    <table border="1" id="cas7">
        <tr>
            <td>rechin</td>
            <td />
        </tr>
        <tr>
            <td>amarillo</td>
            <td />
        </tr>
        <tr>
            <td>this shouldn't been</td>
            <td>deleted</td>
        </tr>
    </table>
</html>

The wanted correct result is produced:

<html>
    <table border="1" id="cas6">
        <tr>
            <td>
                <table border="1">
                    <tr>
                        <td>rechin</td>
                    </tr>
                    <tr>
                        <td>amarillo</td>
                    </tr>
                </table></td>
        </tr>
    </table>
    <table border="1" id="cas7">
        <tr>
            <td>rechin</td>
            <td></td>
        </tr>
        <tr>
            <td>amarillo</td>
            <td></td>
        </tr>
        <tr>
            <td>this shouldn't been</td>
            <td>deleted</td>
        </tr>
    </table>
</html>
Dimitre Novatchev
Monomachus