You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML表格rowspan/colspan错误校验及XSLT修正方案咨询

修正含错误rowspan的XML表格XSLT处理逻辑,解决内容丢失问题

问题概述

输入的XML表格存在错误的rowspan属性(例如<td rowspan="3">36</td>),用现有XSLT处理后,输出表格丢失了大量内容:

  • <thead>里的<th align="center">6</th>、<th align="center">7</th>、<th align="center">8</th>
  • <tbody>里的<td>40</td>、<td>41</td>、<td>30</td>、<td>31</td>等
    需要修正XSLT代码,生成保留原始错误格式的表格,用于后续校验。

修正思路

核心是不修正原始表格的rowspan逻辑错误,仅做节点和内容的原样映射,不跳过或自动补全单元格,确保所有原始内容都被保留。

修正后的XSLT代码

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>

  <!-- 匹配表格根节点,原样复制属性并处理子节点 -->
  <xsl:template match="table">
    <table>
      <xsl:if test="@*">
        <xsl:copy-of select="@*"/>
      </xsl:if>
      <xsl:apply-templates select="thead|tbody|tfoot"/>
    </table>
  </xsl:template>

  <!-- 处理表头、表体、表尾容器 -->
  <xsl:template match="thead|tbody|tfoot">
    <xsl:copy>
      <xsl:apply-templates select="tr"/>
    </xsl:copy>
  </xsl:template>

  <!-- 处理表格行,复制属性后处理单元格 -->
  <xsl:template match="tr">
    <tr>
      <xsl:if test="@*">
        <xsl:copy-of select="@*"/>
      </xsl:if>
      <xsl:apply-templates select="th|td"/>
    </tr>
  </xsl:template>

  <!-- 处理表头单元格和数据单元格,完整复制属性与内容 -->
  <xsl:template match="th|td">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- 复制文本节点内容 -->
  <xsl:template match="text()">
    <xsl:value-of select="."/>
  </xsl:template>
</xsl:stylesheet>

代码说明

  • 全程采用原样复制逻辑,不对任何rowspan属性做校验或调整,完全保留原始XML的错误结构
  • 对表格的各级节点(table、thead/tbody/tfoot、tr、th/td)分别匹配处理,确保节点本身、所有属性及子内容都被完整复制
  • 避免了原有XSLT中可能存在的“跳过单元格”或“自动修正rowspan”逻辑,从而防止内容丢失

效果验证

用这段XSLT处理后,生成的HTML表格会:

  • 完整保留表头的<th align="center">6</th>、<th align="center">7</th>、<th align="center">8</th>
  • 不丢失<tbody>里的<td>40</td>、<td>41</td>、<td>30</td>、<td>31</td>等内容
  • 原样保留错误的<td rowspan="3">36</td>属性,满足后续格式校验的需求

内容的提问来源于stack exchange,提问作者BWin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 17:55:19