Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   Replacing elements (http://www.velocityreviews.com/forums/t303811-replacing-elements.html)

miroslaw.rusin@mensa.org.pl 06-21-2006 05:06 PM

Replacing elements
 
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


Martin Honnen 06-21-2006 05:23 PM

Re: Replacing elements
 


miroslaw.rusin@mensa.org.pl wrote:


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


--

Martin Honnen
http://JavaScript.FAQTs.com/

miroslaw.rusin@mensa.org.pl 06-21-2006 05:40 PM

Re: Replacing elements
 
Problem solved. The trick is to simply use:

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


Joe Kesselman 06-21-2006 05:44 PM

Re: Replacing elements
 
miroslaw.rusin@mensa.org.pl wrote:
> 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.

miroslaw.rusin@mensa.org.pl 06-22-2006 07:02 AM

Re: Replacing elements
 
Thank you, you're 100% right.



All times are GMT. The time now is 03:53 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.