我想删除 XSLT 中的标签,但保留其中的所有内容。
我有这个来源:
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<div>
<p>foo</p>
<div>we want any divs found in here</div>
<p>we want to keep everything</p>
<p>except the div that follows DOCUMENT</p>
</div>
</DOCUMENT>
并且想要这样的输出:
<?xml version="1.0" encoding="UTF-8"?>
<DOCUMENT>
<p>foo</p>
<div>we want any divs found in here</div>
<p>we want to keep everything</p>
<p>except the div that follows DOCUMENT</p>
</DOCUMENT>
不需要的<div>
始终遵循<DOCUMENT>
这是唯一的<div>
我想删除。
我可以使用这个 XSLT 来做到这一点
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/DOCUMENT/div">
<xsl:apply-templates select="@*|node()"/>
</xsl:template>
</xsl:stylesheet>
问题:我不想使用身份转换模板执行此操作,因为我正在处理“拉”转换,而不是推送。但我很难找到替代方案。
最佳答案
那么,当您使用拉式样式表时,简单地选择 DOCUMENTS/div 下的所有元素不是最简单的方法吗?
示例:
<xsl:template match="/">
<DOCUMENT>
<xsl:for-each select="DOCUMENT/div/*">
<xsl:copy-of select="."/>
</xsl:for-each>
</DOCUMENT>
</xsl:template>
尝试此操作时,我得到以下结果:
<DOCUMENT>
<p>foo</p>
<div>we want any divs found in here</div>
<p>we want to keep everything</p>
<p>except the div that follows DOCUMENT</p>
</DOCUMENT>
关于xml - XSLT 删除标签但保留内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16441127/