XSLT: Copy Element with its surrounding Elements (Recursive Loop Error)

38 views Asked by At

Im struggling to find a solution for this problem because i end up in a recursive loop.

XML Source Example:

<runtime xmlns="http://xxx/v1"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="xxx.xsd">
    <messages>
        <message>
            <severity>debug</severity>
            <source>Client</source>
            <subject>Subject</subject>
            <body>Body</body>
        </message>
    </messages>

And i need to put the Messages element inside itself in the body element

  <runtime xmlns="http://xxx/v1"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="xxx.xsd">
        <messages>
            <message>
                <severity>debug</severity>
                <source>Client</source>
                <subject>Subject</subject>

                <body>

                <messages>
                    <message>
                        <severity>debug</severity>
                        <source>Client</source>
                        <subject>Subject</subject>
                        <body>Body</body>
                </message>
                </messages>

               </body>

            </message>
        </messages>

I can not come up with a working solution without ending up in loop error. Any help aprriciated.

2

There are 2 answers

0
Fred Laurent On BEST ANSWER

You have to run a copy template except for the <body> element. For this special tag, make another copy template with a name to drive the transformation from your new start and use another path in ordrer to avoid the loop.

Classic copy template execpt for body

<xsl:template match="node()[local-name()!='body'] | @*">
    <xsl:copy>
        <xsl:apply-templates select="node() | @*"/>
    </xsl:copy>
</xsl:template>

For the body element, start copying from the beginning by assigning another path (with the mode)

 <xsl:template match="*[local-name()='body']">
    <xsl:copy>
        <xsl:apply-templates select="/*:runtime/*:messages" mode='embedded'/>
    </xsl:copy>
</xsl:template>

And copy the tags into the body element with another copy template

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

As a result :

<?xml version="1.0" encoding="UTF-8"?>
<runtime xmlns="http://xxx/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="xxx.xsd">
    <messages>
        <message>
            <severity>debug</severity>
            <source>Client</source>
            <subject>Subject</subject>
            <body>
                <messages>
                    <message>
                        <severity>debug</severity>
                        <source>Client</source>
                        <subject>Subject</subject>
                        <body>Body</body>
                    </message>
                </messages>
            </body>
        </message>
    </messages>
</runtime>
0
michael.hor257k On

Here's one way you could look at it:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:v1="http://xxx/v1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

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

<xsl:template match="v1:body">
    <xsl:copy>
        <xsl:copy-of select="../.."/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

As an intellectual exercise, in XSLT 2.0 you could do:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xpath-default-namespace="http://xxx/v1">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

<!-- identity transform -->
<xsl:template match="@*|node()" mode="#all">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()" mode="#current"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="body">
    <xsl:copy>
        <xsl:apply-templates select="../.." mode="copy"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="body" mode="copy">
    <xsl:copy>
        <xsl:apply-templates/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>