如何复制整个元素但只删除部分子元素? 我想复制div#about
但我想从中删除table
元素。
输入网页:
<html>
<body>
<div class="content-header">
<h1>Title</h1>
</div>
<div id="about">
<h1>About</h1>
<table>...</table>
<p>Bla bla bla</p>
<table>...</table>
<p>The end</p>
</div>
</body>
</html>
XSLT:
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<div class="article">
<h1>
<xsl:value select="//div[@class='content-header']/h1/text()"/>
</h1>
<div>
<xsl:copy-of select="//div[@id='about']"/>
<!-- Here should render the entire div#about without the tables -->
</div>
</div>
</xsl:template>
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
</xsl:transform>
首先将标识模板添加到 XSLT
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
(或者,如果您使用的是 XSLT 3.0,则可以改用<xsl:mode on-no-match="shallow-copy"/>
(
然后添加另一个模板以忽略table
元素
<xsl:template match="div[@id='about']/table" />
最后,将您的xsl:copy-of
替换为xsl:apply-templates
以允许匹配这些模板,从而确保table
元素不会被复制。
试试这个 XSLT
<xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="div[@id='about']/table" />
<xsl:template match="/">
<div class="article">
<h1>
<xsl:value-of select="//div[@class='content-header']/h1/text()"/>
</h1>
<div>
<xsl:apply-templates select="//div[@id='about']"/>
</div>
</div>
</xsl:template>
</xsl:transform>