问题描述
我是XSLT的新手,我需要将所有相邻HTML表的行(具有相同的结构)放在一个表中,只需在第一个表中附加以下表的所有行:
I am new to XSLT and I have the requirement of bringing all rows of adjacent HTML tables, having the same structure, in a single table, simply by appending all rows of following tables in the first table encountered:
<div>
<span class="title">sample</span>
<br/>
some text node1
<br/>
some text node2
<p/>
<table class="class1">
<tbody>
<tr>
<td>L1:</td>
<td>C1</td>
</tr>
</tbody>
</table>
<p/>
<span class="section">Section1</span>
<p/>
<table class="class1">
<tbody>
<tr>
<td>L2:</td>
<td>C2</td>
</tr>
</tbody>
</table>
<table class="class1">
<tbody>
<tr>
<td>L3:</td>
<td>C3</td>
</tr>
</tbody>
</table>
<table class="class1">
<tbody>
<tr>
<td>L4:</td>
<td>C4</td>
</tr>
</tbody>
</table>
<span class="section">Section2</span>
<p/>
<table class="class1">
<tbody>
<tr>
<td>L5:</td>
<td>C5</td>
</tr>
</tbody>
</table>
<table class="class2">
<tbody>
<tr>
<td>L6:</td>
<td>C6</td>
</tr>
</tbody>
</table>
</div>
结果输出应该与@ class =class1合并相邻的表。
The resulting output, should merge adjacent tables with @class="class1".
<div>
<span class="title">sample</span>
<br/>
some text node1
<br/>
some text node2
<p/>
<table class="class1">
<tbody>
<tr>
<td>L1:</td>
<td>C1</td>
</tr>
</tbody>
</table>
<p/>
<span class="section">Section1</span>
<p/>
<table class="class1">
<tbody>
<tr>
<td>L2:</td>
<td>C2</td>
</tr>
<tr>
<td>L3:</td>
<td>C3</td>
</tr>
<tr>
<td>L4:</td>
<td>C4</td>
</tr>
</tbody>
</table>
<span class="section">Section1</span>
<p/>
<table class="class1">
<tbody>
<tr>
<td>L5:</td>
<td>C5</td>
</tr>
</tbody>
</table>
<table class="class2">
<tbody>
<tr>
<td>L6:</td>
<td>C6</td>
</tr>
</tbody>
</table>
</div>
任何想法如何实现?我一直在尝试使用群组相邻,但我没有得到预期的结果,我不知道是什么让我失望。
Any ideas how this can be realized? I have been trying to do this using group-adjacent, but I'm not getting the intended result, and I don't know what's missing me.
- 编辑:
-
这是我的尝试(工作不正常)。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table[@class='class1']">
<xsl:choose>
<xsl:when test="not(preceding-sibling::*[1][self::table])">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="preceding-sibling::*[1]/@class='class1'" />
<xsl:when test="following-sibling::*[1]/@class='class1'" >
<table class="class1">
<tbody>
<xsl:apply-templates select="tbody/*"/>
<xsl:for-each select="following-sibling::*">
<xsl:if test="self::table[@class='class1']">
<xsl:apply-templates select="tbody/*"/>
</xsl:if>
</xsl:for-each>
</tbody>
</table>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
注意
如果可能,请帮助我在XSLT1.0,增强我的尝试和/或XSLT2.0中做到这一点。
NOTEIf possible help me do that both in XSLT1.0, enhancing my attempt, and/or XSLT2.0.
推荐答案
使用XSLT 2.0:
Using XSLT 2.0:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body | div"><!-- need to list other container elements here -->
<xsl:copy>
<xsl:for-each-group select="node() except text()[not(normalize-space())]" group-adjacent="boolean(self::table[@class = 'class1'])">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<table class="{@class}">
<xsl:apply-templates select="thead"/>
<tbody>
<xsl:apply-templates select="current-group()/tbody/tr"/>
</tbody>
</table>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
这是第一个版本的修正,应该确保包含多个空格的所有文本节点都是也复制了。但是它假设你想要分组的那些表之间只有空格。
This is a correction of the first version which should ensure that all text nodes containing more than white space are also copied. But it assumes that between those tables you want to group there is solely white space.
这篇关于XSLT将相邻结构的相邻HTML表中的行合并到一个表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!