Zaphod
Zaphod

Reputation: 7290

xsl:copy-of just the content without the node

I have a little problem with xsl:copy-of, because I only want to copy the content of the node, not the node itself:

In the XML:

<parent>    
    <node>Hello, I'm a <b>node</b>!!!</node>
</parent>

In the XSL:

<xsl:template match="parent">
    <tr>
        <td><xsl:copy-of select="node"/></td>
    </tr>
</xsl:template>

The result:

<tr>
    <td><node>Hello, I'm a <b>node</b>!!!</node></td>
</tr>

The expected result :

<tr>
    <td>Hello, I'm a <b>node</b>!!!</td>
</tr>

The problem is that if I use xsl:value-of, I loose the <b></b> !!!

Upvotes: 5

Views: 2973

Answers (1)

Ian Roberts
Ian Roberts

Reputation: 122414

You could use

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

It looks a bit odd because the element name is also node but what the node() selector does is select all child elements, text nodes, comment nodes and processing instructions from inside the appropriate node(s) (in this case all child elements called node in the current context element).

node() does not select attributes, so if you started with

<parent>    
    <node attr="foo">Hello, I'm a <b>node</b>!!!</node>
</parent>

then <td><xsl:copy-of select="node/node()"/></td> would produce

<td>Hello, I'm a <b>node</b>!!!</td>

If instead you said <td><xsl:copy-of select="node/node() | node/@*"/></td> then you'd get

<td attr="foo">Hello, I'm a <b>node</b>!!!</td>

Upvotes: 8

Related Questions