2012-03-28 47 views
0

如何複製xml文檔跳過一些頂級節點。例如:用xslt複製xml跳過頂級節點

輸入:

<root> 
<subroot> 
    <nodeX id="1"> 
    <!-- inner structure --> 
    </nodeX> 
    <nodeX id="2"> 
    <!-- inner structure --> 
    </nodeX> 
    <!-- other nodes --> 
    </subroot> 
<root> 

輸出:

<nodeX id="1"> 
    <!-- inner structure --> 
    </nodeX> 
    <nodeX id="2"> 
    <!-- inner structure --> 
    </nodeX> 

回答

3
<xsl:template match="@* | node()"> 
    <xsl:copy> 
    <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="root | subroot"> 
    <xsl:apply-templates/> 
</xsl:template> 

應該。如果你想或需要更通用的東西,然後使第二個模板

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

是否有可能跳過另一個節點是根節點的子節點?這些節點匹配/ root/subrootX |/root/subrootY但我不想複製它們。 – Max 2012-03-28 09:54:30

+1

只需將不想複製的圖案添加到第二個模板匹配圖案,例如''。或者,如果您知道您不想複製任何根元素的任何子元素,請使用已發佈的通用'match =「/ * |/*/*」'。 – 2012-03-28 10:04:11