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 September 26th, 2011, 12:00 PM
Registered User
 
Join Date: Sep 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default Add an attribute according to value

Hi,
I must add an attribute to an already existing node (with children) according to one of the other attributes' value:

<node attr1="1"><child/></node>

if test @attr1=1 add attr2="y"
else add attr2="n"

result:
<node attr1="1" attr2="y"><child/></node>

the problem is that I can't add attributes due to node's child:
Code:
<xsl:choose>
			<xsl:when test="@attr1='1'">
			  <xsl:attribute name="attr2">y</xsl:attribute>
			</xsl:when>
			<xsl:otherwise>
				<xsl:attribute name="atte2">n</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
reports an error:
xsl:attribute: Cannot add attributes to an element if children have been already added to the element.

and copying makes a mess of things:
Code:
<xsl:choose>
			<xsl:when test="@attr1='1'"><xsl:copy>
			<xsl:copy-of select="@*"/>
				<xsl:attribute name="attr2">y</xsl:attribute>
			<xsl:copy-of select="*"/>
		</xsl:copy></xsl:when>
			<xsl:otherwise><XSL-copy>
			<xsl:copy-of select="@*"/>
				<xsl:attribute name="attr2">n</xsl:attribute>
			<xsl:copy-of select="*"/>
		</xsl:copy></xsl:otherwise>
		</xsl:choose>
this generates:
<node attr1="1"><child/><node attr1="1" attr2="y"></node></node>

What do I do???

thank you,
Eran
 
Old September 26th, 2011, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well you need to change your code to ensure that you first create all attribute nodes of the "node" element, then all children.
It is hard to tell more as you have not show any context (e.g. template match).
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old September 26th, 2011, 07:31 PM
Registered User
 
Join Date: Sep 2011
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thank you Martin.

I'm very new to XSL, in fact this is the first time I've touched it.
I inherited some infrastructure and am fumbling with it.

I'll look into templates and see how I can use then to assist me.

If you have any useful advice I'll be happy to read it.

Regards,
Eran





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add an attribute to each element bartnowa XSLT 3 November 8th, 2007 12:12 PM
How to add attribute to unbounded element 2BOrNot2B XML 4 January 16th, 2007 03:42 PM
XSLT, add attribute ik XSLT 6 August 22nd, 2006 11:03 AM
How to add an attribute crossedge XSLT 5 March 16th, 2006 06:31 AM
Add an attribute to existing table peter_budo SQL Language 9 April 24th, 2005 01:35 PM





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