Html XSLT:在父节点之后移动子节点
我目前正在使用XSLT清理和修改一些(导出的)HTML。到目前为止效果还不错 但是我需要修改一个表,以便将tfoot复制到表之外 输入:(由Adobe Indesign导出):Html XSLT:在父节点之后移动子节点,html,xml,xslt,parent,Html,Xml,Xslt,Parent,我目前正在使用XSLT清理和修改一些(导出的)HTML。到目前为止效果还不错 但是我需要修改一个表,以便将tfoot复制到表之外 输入:(由Adobe Indesign导出): 东西 更多的东西 一些页脚的东西 更多页脚 东西 更多的东西 我的预期产出: <table> <thead> <tr> <td>Stuff</td> <td>More Stuff</td>
东西
更多的东西
一些页脚的东西
更多页脚
东西
更多的东西
我的预期产出:
<table>
<thead>
<tr>
<td>Stuff</td>
<td>More Stuff</td>
</tr>
</thead>
<tbody>
<tr>
<td>Stuff</td>
<td>More Stuff</td>
</tr>
</tbody>
</table>
<div class="footer">
Some footer things
Even more footer
</div>
东西
更多的东西
东西
更多的东西
一些页脚的东西
更多页脚
我在XSL中做的第一件事是复制所有内容:
<xsl:template match="*|@*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:template>
但下一步是什么?XSLT甚至可以做到这一点吗?提前谢谢。试试以下方法:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table">
<xsl:copy>
<xsl:apply-templates select="thead"/>
<xsl:apply-templates select="tbody"/>
</xsl:copy>
<xsl:apply-templates select="tfoot"/>
</xsl:template>
<xsl:template match="tfoot">
<div class="footer">
<xsl:apply-templates select="tr/td/text()"/>
</div>
</xsl:template>
</xsl:stylesheet>
我不确定您希望如何准确地排列页脚div
;您可能希望使用xsl:for-each
在文本节点之间插入分隔符
还要注意,这里的结果不是格式良好的XML,因为它没有单个根元素。为什么不在导出后编辑HTML?另外,
tfoot
与div.footer
不同,这只是一个非常简化的示例谢谢,这帮了我很大的忙。“真实”标记要复杂得多,我只是为了我的问题简化了它。
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="table">
<xsl:copy>
<xsl:apply-templates select="thead"/>
<xsl:apply-templates select="tbody"/>
</xsl:copy>
<xsl:apply-templates select="tfoot"/>
</xsl:template>
<xsl:template match="tfoot">
<div class="footer">
<xsl:apply-templates select="tr/td/text()"/>
</div>
</xsl:template>
</xsl:stylesheet>