2011-02-11 31 views
4

問題的複製 輸入:XSLT複製的,我怎麼能跳過子節點,同時通過XSL複製:複製的

<Rel> 
    <IRel UID1="3a4d1d2909d0" UID2="35fe61082294" DefUID="AssetSupplier" /> 
    <IObject UID="3a4d1d2909d0.AssetSupplier.35fe61082294" /> 
    <SPXSupplier> 
     <ISPFOrganization /> 
     <ISPFAdminItem /> 
     <IObject UID="b73ebb87-cd36-4c25-b9ed-35fe61082294" 
       Description="local supplier made in form (10C)" 
       Name="CASTROL1200" /> 
     <ISupplierOrganization /> 
    </SPXSupplier> 
</Rel> 

輸出: 我只是想跳過SPXSupplier及其子節點在我輸出

<Rel> 
    <IRel UID1="3a4d1d2909d0" UID2="35fe61082294" DefUID="AssetSupplier" /> 
    <IObject UID="3a4d1d2909d0.AssetSupplier.35fe61082294" /> 
</Rel> 

目前我使用的這個副本複製所有的事情,包括孩子, <xsl:copy-of select="self::node()"/>

我只想要<Rel><IRel><IObject>標籤。排除其他東西。

+0

[XSL:如何複製樹,但刪除某些節點?]的可能重複(http://stackoverflow.com/questions/1137563/xsl-how-to-copy-a-tree-but-removing-一些節點) – 2011-02-12 13:49:14

回答

2

xsl:copy-of複製整個子樹。要排除某個SPXSupplier元素,你可以用下面的辦法:

<xsl:template match="//Rel"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|IRel|IObject"/> 
    </xsl:copy> 
</xsl:template> 

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

這一個似乎是好的我會嘗試這個謝謝亞歷克斯 – abhatti 2011-02-14 09:02:10

6

這裏是亞歷克斯的答案的改進。

<xsl:template match="SPXSupplier"/> 

<xsl:template match="*"> 
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:apply-templates/> 
    </xsl:copy> 
</xsl:template> 

爲SPXSupplier空模板意味着當擊中這些元件中的一個時,該元素下的子樹沒有被處理。我還使用了無條件複製屬性的身份模板版本,效率更高。

+0

+1更好的答案。 – Flack 2011-02-11 11:15:55