Replacing elements

M

miroslaw.rusin

Hi mates! :)

The challange is like this: how to replace only one element in the
whole tree to something else?

Example:

Input:
<?xml version="1.0"?>
<html>
<head></head>
<body>
<b> some<u>thing</ul> </b>
</body>
</html>

into:
<?xml version="1.0"?>
<html>
<head></head>
<body>
<strong> some<u>thing</ul> </strong>
</body>
</html>

(<b> -> <strong>) ?

Mirek
 
M

Martin Honnen

The challange is like this: how to replace only one element in the
whole tree to something else?
(<b> -> <strong>) ?

<xsl:template match="b">
<strong>
<xsl:apply-templates select="@* | node()" />
</strong>
</xsl:template>
then you need the identity transformation for the rest
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
</xsl:copy>
</xsl:template>
 
M

miroslaw.rusin

Problem solved. The trick is to simply use:

<xsl:element name="{name()}">
<xsl:apply-templates />
</xsl:element>
 
J

Joe Kesselman

Problem solved. The trick is to simply use:

<xsl:element name="{name()}">
<xsl:apply-templates />
</xsl:element>

That isn't namespace-sensitive. You want to set the namespace URI as
well. At which point, you've essentially reinvented xsl:copy.

You also probably need to copy attributes.

Martin's answer is a cleaner one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top