XSLT副本 - 如何在通过XSL复制时跳过子节点:copy-of

时间:2011-02-11 08:58:40

标签: xslt

复制问题 输入:

<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>标记。排除其他东西。

2 个答案:

答案 0 :(得分:6)

这是亚历克斯答案的改进。

<xsl:template match="SPXSupplier"/>

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

SPXSupplier的空模板意味着当您点击其中一个元素时,不会处理该元素下面的子树。我还使用了一种无条件复制属性的身份模板版本,效率更高。

答案 1 :(得分: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>