How to copy all child nodes of any type of a template context element

monty picture monty · Jun 1, 2011 · Viewed 72.8k times · Source

I am transforming XML into HTML using XSLT.

I have the following XML structure:

<root>
    <element>
        <subelement>
            This is some html text which should be <span class="highlight">displayed highlighted</span>.
         </subelement>
    </element>
</root>

I use the following template for the transformation:

<xsl:template name="subelement">
  <xsl:value-of select="." />
</xsl:template>

Unfortunately, I lose the <span>-tags.

Is there a way to keep them so the HTML is displayed correctly (highlighted)?

Answer

Emiliano Poggi picture Emiliano Poggi · Jun 1, 2011

The correct way to get the all the contents of the current matching node (text nodes included) is:

    <xsl:template match="subelement">
       <xsl:copy-of select="node()"/>
    </xsl:template>

This will copy everything descendent.