Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2014, 10:16 AM
Registered User
 
Join Date: Oct 2012
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
Default copy-of without the element tag

Hi all,

I need to copy several elements but sometimes add an attribute into the element tag as well. The attribute to look for in the source is always the same and should be copied into a class attribute in the target. Then all contents of the source should be copied into the element. I have tried to achieve this as follows:

<xsl:template match="p|note|propval|strow">
<xsl:element name="{local-name()}">
<xsl:if test="@version!=''">
<xsl:attribute name="class">
<xsl:value-of select="@version"/>
</xsl:attribute>
</xsl:if>
<xsl:copy-of select="."/>
</xsl:element>
</xsl:template>

This gives me an element with the correct class attribute, but with a nested element that has the original stuff in it. So for every <p> I get a <p> with a nested <p>.

If I use <xsl:value-of select="."/> I lose all the child elements. What option am I missing here?

Thanks in advance for your help

Jang
 
Old January 7th, 2014, 11:07 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can use copy-of select="@* | node()" to copy all attributes and all child nodes. But the usual approach would be to delegate further processing to other templates like the identity transformation template and then simply do
apply-templates select="@* | node()"
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to define XSLT tag for particular element only XSLT_Prof XSLT 2 August 1st, 2013 11:12 AM
How to copy new element at child level dishant XSLT 1 January 13th, 2012 03:24 AM
How to find no.of childnodes of a tag element? sachin635 .NET Framework 3.5 0 September 15th, 2008 04:32 AM
Copy element into complexType in XSLT 2BOrNot2B XSLT 3 December 20th, 2006 09:09 AM
How to use xsl:element tag??? NewToXSL XSLT 2 June 8th, 2006 05:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.