2016-06-10 187 views
0

我嘗試了幾種不同的方法將所有子節點傳遞給父父節點,模式匹配僅發生在一個子節點上,但不是遞歸地發生。感謝這裏的任何幫助。如何將子節點與值一起移動到父父節點

<grandparent1> 
    <parent1> 
     <child1>1</child1> 
     <child2>2</child2> 
    </parent2> 
</grandparent1> 

應該成爲

<grandparent1> 
    <child1>1</child1> 
    <child2>2</child2> 
</grandparent1> 

子節點的大小而異。 欣賞這裏的任何幫助

+0

你用什麼語言編程。如果有任何代碼,你能提供嗎?順便說一句,你有在頂部,而在底部。 – FLICKER

+0

http://stackoverflow.com/help/someone-answers –

回答

0

這是很容易做到使用XSLT的遞歸processing model

首先,使用標識變換模板以遞歸方式複製的所有內容,如。然後爲父節點添加一個異常,並讓它們繼續遞歸而不復制它們自己。

XSLT 1.0

<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="parent1 | parent2"> 
    <xsl:apply-templates/> 
</xsl:template> 

</xsl:stylesheet> 

爲了使這個通用的任何父(即2級)節點,事先不知道它的名字,你可以使用:

<xsl:template match="/*/*"> 
    <xsl:apply-templates/> 
</xsl:template> 

爲第二模板。