2013-05-08 104 views
1

我想删除XSLT中的标记,但将所有内容都留在原地。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> 

问题要做到这一点:我不想与身份变换模板,要做到这一点,因为我工作的一个“拉”转变,不推。但我很难找到另一种选择。

回答

3

那么,当您使用拉样式表时,是不是简单地选择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> 
+0

感谢。你的回复给了我需要的微调。有时你长期看问题,你不知道答案在你面前是正确的。 – Paulb 2013-05-08 14:03:09