Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSLT copy-of how I can skip the child node while copying through XSL:copy-of

Tags:

xslt

Question for Copy input:

<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>

Output: I only want to skip SPXSupplier and its child node in my output

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

currently I am using this copy which copy all the things including the child, <xsl:copy-of select="self::node()"/>

I only want <Rel>, <IRel> and <IObject> tags. excluding other stuff.

like image 243
abhatti Avatar asked Feb 26 '23 03:02

abhatti


2 Answers

Here's a refinement of Alex's answer.

<xsl:template match="SPXSupplier"/>

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

The empty template for SPXSupplier means that when you hit one of these elements, the subtree below that element is not processed. I've also used a version of the identity template that copies attributes unconditionally, which is more efficient.

like image 141
Michael Kay Avatar answered Feb 27 '23 15:02

Michael Kay


xsl:copy-of copies the whole subree. To exclude an SPXSupplier element you can use the following approach:

<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>
like image 33
Alex Nikolaenkov Avatar answered Feb 27 '23 17:02

Alex Nikolaenkov