Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

xslt

xslt-1.0

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

like image 262
monty Avatar asked Jun 01 '11 09:06

monty


People also ask

How do I copy nodes in XSLT?

XSLT <xsl:copy-of> The <xsl:copy-of> element creates a copy of the current node. Note: Namespace nodes, child nodes, and attributes of the current node are automatically copied as well! Tip: This element can be used to insert multiple copies of the same node into different places in the output.

What does xsl copy do?

Causes the current XML node in the source document to be copied to the output. The actual effect depends on whether the node is an element, an attribute, or a text node.

How does xsl template match work?

The <xsl:template> element is used to build templates. The match attribute is used to associate a template with an XML element. The match attribute can also be used to define a template for the entire XML document. The value of the match attribute is an XPath expression (i.e. match="/" defines the whole document).

How do I apply a template in XSLT?

The <xsl:apply-templates> element applies a template to the current element or to the current element's child nodes. If we add a "select" attribute to the <xsl:apply-templates> element, it will process only the child elements that matches the value of the attribute.


1 Answers

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.

like image 169
Emiliano Poggi Avatar answered Sep 19 '22 18:09

Emiliano Poggi