Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XSLForum: Main > XSL Coding General

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-04-2008, 03:34 PM
kpg
 
Posts: n/a

Default Setting a a variable to a default value



Hi all,

XSL Novice

In the source XML I have a Display_Count node that is the
number of lines to display in the output HTML generated by
my XSL.

So for example, if <Display_Count>10</Display_Count> I get 10
lines, if <Display_Count>5</Display_Count> I get 5.

I set a variable loop_num to be used as a loop counter later
in the XSL as follows:

<xsl:variable name="loop_num" select="//Display_Count"/>

This works fine.

Now I want to handle the case where the Display_Count node
does not exist in the XML input. That is, I want to set a
default value (say, of 3) for loop_num in that case.

I ran into the problem where a variable cannot be reassigned
and its definition cannot be duplicated in the current scope.

So something like this will not work:

<xsl:choose>
<xsl:when test="not(//Display_Count)">
<xsl:variable name="loop_num" select="1"/>
</xsl:when>
<xsltherwise>
<xsl:variable name="loop_num" select="//Display_Count"/>
</xsltherwise>
</xsl:choose>

How can I set the varable loop_num to some default constant
value if the Display_Count node does not exist or to the value
of the Display_Count node if it does exist?

Thanks
kpg

Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-04-2008, 03:48 PM
Martin Honnen
 
Posts: n/a

Default Re: Setting a a variable to a default value

kpg wrote:

> So something like this will not work:
>
> <xsl:choose>
> <xsl:when test="not(//Display_Count)">
> <xsl:variable name="loop_num" select="1"/>
> </xsl:when>
> <xsltherwise>
> <xsl:variable name="loop_num" select="//Display_Count"/>
> </xsltherwise>
> </xsl:choose>


The other way round works:
<xsl:variable name="loop_num">
<xsl:choose>
<xsl:when test="not(//Display_Count)">3</xsl:when>
<xsltherwise>
<xsl:value-of select="//Display_Count"/>
</xsltherwise>
</xsl:choose>
</xsl:variable>

With XSLT 2.0 you could use the much shorter
<xsl:variable name="loop_num"
select="if (not(//Display_Count)) then 3 else
//Display_Count"/>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 03:27 PM.
Style Developed by Epic Designz