2011-03-24 58 views
1

我有一些XML有一个额外的元素,我希望它消失。输入XML:选择特定的子元素节点树使用xslt输出

<top><middle><bottom><!-- other elements --><stuff/></bottom></middle></top> 

输出所需:

<top><bottom><!--other elements --><stuff/></bottom></top> 

(注意 “中间” 元素已经从节点树剪断)

如何随意剪断了一个元素,而无需创建源中每个可能元素的模板级联?有没有办法让一个给定点的所有元素和子元素都通过?包括XML标签,属性和内容?

搜索我已经提到使用<xsl:copy>但它不起作用 - "node()|@*"只返回内容和属性值,而不是实际的子元素XML树。

如何在XSLT 1或2中执行此操作?我现在这样做的方式是为每个元素创建一个模板树,但“东西”?

回答

2

使用Identity与覆盖变换为要删除的元素:

<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="middle"> 
     <xsl:apply-templates/> 
    </xsl:template> 
</xsl:stylesheet> 
+0

+1 - 这就是它。 – Tomalak 2011-03-24 22:40:54

+0

对于正确的答案+1。 – 2011-03-25 12:32:48

+0

@Alejandro - 是的,好点。我会更新。 – 2011-03-27 00:24:37