如何使用XSLT删除HTML表中的空列?

如何使用XSLT删除HTML表中的空列?,xslt,html-table,Xslt,Html Table,如何使用XSLT删除HTML表中的空列,并具有如下功能: <table id="cas6"> <tr> <td /> <td> <table> <tr> <td>rechin</td> <td /> </tr>

如何使用XSLT删除HTML表中的空列,并具有如下功能:

<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>

里钦 阿马里洛 里钦 阿马里洛 这不应该 删除


要删除空列,这意味着删除Xth位置中所有tr中为空的td

如果我对问题的理解正确,对于给定的表,如果nth列中的所有条目都为空,是否要从表中删除该列

那么试试这个XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <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>


这是“identity”转换,但当它与TD元素匹配时,它首先获取列号,然后检查其他行中的任何其他列是否为空。如果它在同一列中找到任何非空单元格,它将复制TD元素,否则它将被忽略。

这就是为我工作的XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <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>


这里有一个非常简单的解决方案:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>
<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>

在提供的XML文档上应用此转换时(格式正确):


里钦
阿马里洛
里钦
阿马里洛
这不应该
删除
生成所需的正确结果

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <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>
<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>

里钦
阿马里洛
里钦
阿马里洛
这不应该
删除

这是不明确的。请显示您想要的确切输出。迪米特里·诺瓦切夫显示了我想要的输出。Rechin 2 Marillo 2这不应该删除