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 November 1st, 2007, 06:52 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default Using Variable for xsl:element Name

Hi all,

I have some XSLT code that, based on certain conditions, sets a variable "tag" to one of three string values (see below)
Code:
<xsl:choose>
<xsl:when test="test1">
   <xsl:variable name="tag" select="'aaa'" />
</xsl:when>
<xsl:when test="test2">
   <xsl:variable name="tag" select="'bbb'" />
</xsl:when>
<xsl:otherwise>
   <xsl:variable name="tag" select="'ccc'" />
</xsl:otherwise>
</xsl:choose>
I then want to use the value of this "tag" variable as the name of an element, i.e.:
Code:
<xsl:element name="{$tag}">
   some text
</xsl:element>
However when I test this it claims it cannot find the variable "$tag".

I have also tried putting the xsl:choose code in a separate template that just outputs the "tag" value as text, and then calling that template within the definition of the "tag" variable just before it is used in xsl:element.
In this case it says "Illegal value used for attribute name: bbb" (bbb being the value of "tag").

Any ideas how to get the value of $tag as the xsl:element name.
Thanks,


Neil Belch
Senior Software Technician
CDL

The views opinions and judgements expressed in this message are solely those of
the author. The message contents have not been reviewed or approved by CDL.
__________________
Neil Belch
Technical Officer
CDL

The views opinions and judgements expressed in this message are solely those of the author. The message contents have not been reviewed or approved by CDL.
 
Old November 1st, 2007, 06:59 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Ha, great first post, I've managed to fix it almost immediately myself!

For anyone interested, the solution was to create the separate template, but I didn't realise that XSLT keeps all whitespace and due to my code formatting I had line breaks which were causing the "illegal attribute" error.


Neil Belch
Senior Software Technician
CDL

The views opinions and judgements expressed in this message are solely those of
the author. The message contents have not been reviewed or approved by CDL.
 
Old November 1st, 2007, 07:01 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

You can't use <xsl:variable> like that - you have actually defined three variables, all of which go out of scope as soon as the <xsl:choose> finishes.

What you want is this:

<xsl:variable name="tag">
  <xsl:choose>
    <xsl:when test="test1">
      <xsl:value-of select="'aaa'"/>
   </xsl:when>
   ... etc ...
</xsl:variable>

/- Sam Judson : Wrox Technical Editor -/
 
Old November 1st, 2007, 11:29 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Ah I see, that explains the variable not being found.
My code is now:
Code:
<xsl:variable name="tag">
<xsl:call-template name="gettag">
<xsl:with-param name="root" select="$root" />
</xsl:call-template>
</xsl:variable>

<xsl:element name="{$tag}">
 some code..
</xsl:element>
The "root" param and variable are set elsewhere...

Within the "gettag" template a test outputs specific text which becomes the value of the "tag" variable and is subsequently created as an tag using xsl:element.

This seems to work fine..


Neil Belch
Senior Software Technician
CDL

The views opinions and judgements expressed in this message are solely those of
the author. The message contents have not been reviewed or approved by CDL.
 
Old November 1st, 2007, 11:51 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

As an aside, in XSLT 2.0 you could replace the whole variable/template with a <xsl:function> call in place of the name="{$tag}" attribute.

/- Sam Judson : Wrox Technical Editor -/
 
Old November 2nd, 2007, 04:28 AM
Authorized User
 
Join Date: Nov 2007
Posts: 31
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Unfortunately we're using 1.0, but if we ever upgrade I'll bear it in mind, cheers.


Neil Belch
Senior Software Technician
CDL

The views opinions and judgements expressed in this message are solely those of
the author. The message contents have not been reviewed or approved by CDL.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL(T) problems with xsl:element pan69 XSLT 3 December 13th, 2007 06:24 AM
ASSIGNING A JAVA SCRIPT VARIABLE TO A XSL VARIABLE SOMANATHAN10 XSLT 1 February 21st, 2007 04:26 AM
xsl:variable holding name of an xsl:param perissos XSLT 0 December 5th, 2006 07:09 AM
The difference between xsl:variable and xsl:param NEO1976 XML 2 July 24th, 2006 06:05 AM
pass java variable to xsl variable kathy1016cats XSLT 1 June 14th, 2006 06:23 PM





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