1

我需要能够使用 html 样式的表格数据检查 xml,以确保它是“矩形”的。例如这是矩形(2x2)

<table>
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
  </tr>
</table>

这不是

<table>
  <tr>
    <td>Foo</td>
    <td>Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
  </tr>
</table>

这因行和列跨度以及我需要接受两种标记样式的事实而变得复杂,其中跨单元格包含为空td 省略跨单元格。

<!-- good (3x2), spanned cells included -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td/>
    <td rowspan="2">Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
    <td/>
  </tr>
</table>

<!-- also good (3x2), spanned cells omitted -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td rowspan="2">Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
  </tr>
</table>

这里有一堆坏表的例子,如何处理它们是模棱两可的

<!-- bad, looks like spanned cells are included but more cells in row 1 than 2 -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td/>
    <td rowspan="2">Bar</td>
    <td>BAD</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
    <td/>
  </tr>
</table>

<!-- bad, looks like spanned cells are omitted but more cells in row 1 than 2 -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td rowspan="2">Bar</td>
    <td>BAD</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
  </tr>
</table>

<!-- bad, can't tell if spanned cells are included or omitted -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td rowspan="2">Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
    <td/>
  </tr>
</table>

<!-- bad, looks like spanned cells are omitted but a non-emtpy cell is overspanned -->
<table>
  <tr>
    <td colspan="2">Foo</td>
    <td rowspan="2">Bar</td>
  </tr>
  <tr>
    <td>Baz</td>
    <td>Qux</td>
    <td>BAD</td>
  </tr>
</table>

对于这个问题,我已经有了一个有效的 XSLT 2.0 解决方案,其中涉及将数据规范化为“包含的跨单元格”样式然后进行验证,但是,我的解决方案很麻烦,并且对于面积超过 1000 个单元格的表格开始表现不佳。我的规范化和验证例程涉及按顺序迭代单元格并传递应该由跨度创建的单元格参数,并在我在表格中传递它们的坐标时插入它们。我对他们中的任何一个都不满意。

我正在寻找有关实现此验证的更聪明方法的建议,希望在大型表上具有更好的性能配置文件。为了简单起见,我需要考虑但从示例中省略,它们可以在任何答案中包含th或忽略tdth我不检查thead,tbody和/或tfoot是否具有相同的宽度,这也可以包含或省略。我目前正在使用 XSLT 2.0,但如果 3.0 解决方案明显优于 2.0 中实现的解决方案,我会对它们感兴趣。

4

1 回答 1

0

我认为这类问题不适合 XSLT——尤其是当您必须处理非常大的表时。

我建议使用程序语言开发一个解决方案——也许使用 XSLT 对 XML 进行预处理或后处理。

于 2012-12-02T21:47:19.403 回答