【问题标题】:How to delete an empty column in HTML table using XSLT?如何使用 XSLT 删除 HTML 表中的空列?
【发布时间】:2012-01-04 03:27:11
【问题描述】:

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

rechin
阿马里洛
rechin
阿马里洛
这不应该是 已删除

要删除空列,也就是说要删除 Xth 位置的所有 tr 中为空的 td

【问题讨论】:

  • 这是模棱两可的。请显示您正在寻找的确切输出。
  • Dimitre Novatchev 显示了我要查找的输出。

标签: xslt html-table


【解决方案1】:

这是一个非常简单的解决方案

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

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

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

产生想要的正确结果

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

【讨论】:

    【解决方案2】:

    这是对我有用的 XSLT。

    【讨论】:

      【解决方案3】:

      如果我对问题的理解是正确的,对于给定的表,如果第 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 元素,否则将其忽略。

      【讨论】:

      •  
        1rechin 2
        1amarillo 2
        1这不应该被删除 2删除
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      • 2013-04-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多