Search code examples
xmlxsltnodes

XSLT remove nodes within nodes of the same name


I've got an XML document.

It's got loads of para nodes inside other para nodes.

I want to remove the ones inside and keep the inner text joining it to the other para nodes inner text.

Example XML

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id="1234">This is the inner text <para> it needs joining together</para> maybe with other text</para>
</h1>
</document>

Desired output

<document>
<head>
<front>The front page</front>
</head>
<h1>
<para id='1234'>This is the inner text it needs joining together maybe with other text</para>
</h1>
</document>

Solution

  • This is rather trivial:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="para/para">
        <xsl:apply-templates/>
    </xsl:template>
    
    </xsl:stylesheet>