xslt - 分组元素和后续元素
xslt - grouping element and following element
我有以下结构。也就是说——我可能有如下结构
输出来自 CMS,通常在 table 之前和 table 之后有文本。为了使转换的第二阶段起作用,将 table 之前和 table 之后的段落包装在一个元素中是必不可少的。
<?xml version="1.0" encoding="UTF-8"?>
<div>
<p>some text here</p>
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</div>
我的目标是获得如下所示的输出,其中开头的第一段有自己的包装。然后 paragraph + table + paragraph 的组合被放入包装器中。
文件末尾可能有一些段落,这些段落也应该换行。
<?xml version="1.0" encoding="UTF-8"?>
<body>
<div>
<p>some text here</p>
</div>
<div class="table">
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
</div>
<div class="table">
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>another text at the end of the table</p>
</div>
</body>
我正在玩弄前后 + parent。任何正确方向的指示都会有很大帮助
我仅限于 XSLT 1.0。
我认为问题不在于 well-defined。给定某些假设(应该是 self-evident),并给定一个 well-formed 输入,例如:
XML
<body>
<p>some text here</p>
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</body>
你可以这样做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="p[not(preceding-sibling::*[1][self::figure] or following-sibling::*[1][self::figure])]" mode="stand-alone"/>
<xsl:for-each select="figure">
<div class="table">
<xsl:copy-of select="preceding-sibling::*[1][self::p]"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::*[1][self::p]"/>
</div>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="p" mode="stand-alone">
<div>
<xsl:copy-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
获得:
结果
<body>
<div>
<p>some text here</p>
</div>
<div class="table">
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
</div>
<div class="table">
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</div>
</body>
我有以下结构。也就是说——我可能有如下结构
输出来自 CMS,通常在 table 之前和 table 之后有文本。为了使转换的第二阶段起作用,将 table 之前和 table 之后的段落包装在一个元素中是必不可少的。
<?xml version="1.0" encoding="UTF-8"?>
<div>
<p>some text here</p>
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</div>
我的目标是获得如下所示的输出,其中开头的第一段有自己的包装。然后 paragraph + table + paragraph 的组合被放入包装器中。
文件末尾可能有一些段落,这些段落也应该换行。
<?xml version="1.0" encoding="UTF-8"?>
<body>
<div>
<p>some text here</p>
</div>
<div class="table">
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
</div>
<div class="table">
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>another text at the end of the table</p>
</div>
</body>
我正在玩弄前后 + parent。任何正确方向的指示都会有很大帮助
我仅限于 XSLT 1.0。
我认为问题不在于 well-defined。给定某些假设(应该是 self-evident),并给定一个 well-formed 输入,例如:
XML
<body>
<p>some text here</p>
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</body>
你可以这样做:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="body">
<xsl:copy>
<xsl:apply-templates select="p[not(preceding-sibling::*[1][self::figure] or following-sibling::*[1][self::figure])]" mode="stand-alone"/>
<xsl:for-each select="figure">
<div class="table">
<xsl:copy-of select="preceding-sibling::*[1][self::p]"/>
<xsl:copy-of select="."/>
<xsl:copy-of select="following-sibling::*[1][self::p]"/>
</div>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="p" mode="stand-alone">
<div>
<xsl:copy-of select="."/>
</div>
</xsl:template>
</xsl:stylesheet>
获得:
结果
<body>
<div>
<p>some text here</p>
</div>
<div class="table">
<p>and maybe some text here. it may or may not be here</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>Likely to be a paragraph here</p>
</div>
<div class="table">
<p>And here - often like: table 2, but nothing I can use</p>
<figure>
<table>
<tbody>
<tr>
<td>test</td>
</tr>
</tbody>
</table>
</figure>
<p>and another paragraph at the end of the table</p>
</div>
</body>