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 October 23rd, 2009, 12:51 AM
Registered User
 
Join Date: Oct 2009
Posts: 5
Thanks: 7
Thanked 0 Times in 0 Posts
Default Following XSLT Logic not Working

I have to check a value of a variable in XSLT, if it is null then i
have to set it to default "$" otherwise same value.
so for that i have written the following XSLT Logic:

<XSL:variable name="dummy" select= "ArrayOfBookMark/BookMark
[ShortName='Asp-7041-EndVar1]/Value"/>
<XSL:choose>
<XSL:when test="($dummy!='')">
<XSL:variable name="dummy1" select="$dummy"/>
</when>
<XSL:otherwise>
<XSL:variable name="dummy1" select="'$'"/>
</XSL:variable>
<XSL:otherwise>
</XSL:choose>


Here in "dummy" i am copying the value comming from follwing XML.it is
retriving value correctly in to "dummy".
Condition also checking but the value assignment code for new variable
"dummy1" is not working.


<ArrayOfBookMark>
<BookMark>
<ShortName>Asp-7041-EndVar1</ShortName>
<Value>12345</Value>
</BookMark>
</ArrayOfBookMark>


please help...


Quick response will be appriciated.


-Sanket
[email protected]

Last edited by sanket002; October 23rd, 2009 at 01:10 AM.. Reason: wrong end tags
 
Old October 23rd, 2009, 04:17 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

There are several bad mistakes in your XSLT. You should probably do some reading about the language before you start coding (you're on a Wrox site, and Wrox have some excellent books...)

When X is a node-set, X!='' returns true if X contains a node whose string-value is equal to ''. When X is an empty set, this will always be false.

Usually tests like this are better written using not(): not(X=''). But in this case, I think you are just testing whether X is an empty set, in which case the usual idiom is test="not(X)".

Then, you are creating two variables both called dummy1 in the two branches of your choose, and of course they both go out of scope once you're outside the choose. The correct idiom for this (assuming you're stuck with XSLT 1.0 and can't use 2.0) is

Code:
<xsl:variable name="dummy1">
  <xsl:choose>
    <xsl:when test="....">1</xsl:when>
    <xsl:otherwise>2</xsl:otherwise>
  </xsl:choose>
</xsl:variable>
Remember that XSLT is a functional language and you can't assign values to variables in the usual way.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
sanket002 (October 23rd, 2009)
 
Old October 23rd, 2009, 04:26 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Try the below:<xsl:template match="/">
<xsl:variable name="dummy" select="ArrayOfBookMark/BookMark[ShortName='Asp-7041-EndVar1']/Value"/>
<xsl:choose>
<xsl:when test="($dummy!='')">
<xsl:variable name="dummy1" select="$dummy"/>
<xsl:value-of select="$dummy1"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="dummy1" select="'$'"/>
<xsl:value-of select="$dummy1"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
sanket002 (October 23rd, 2009)
 
Old October 25th, 2009, 11:31 PM
Registered User
 
Join Date: Oct 2009
Posts: 5
Thanks: 7
Thanked 0 Times in 0 Posts
Default Following XSLT Logic not Working

Thanks,

now i have to use this dummy1 in to another templete.

how we can do that?

with "call-templete" and "with-param" it is not possible...

is there any other way??

Please Reply...
 
Old October 26th, 2009, 01:02 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

Your question is not clear. Post your input xml and xsl script and explain what yuo want to achieve or post the needed output xml.
__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
sanket002 (October 27th, 2009)
 
Old October 26th, 2009, 05:09 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>with "call-templete" and "with-param" it is not possible...

Why is it not possible?
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
sanket002 (October 27th, 2009)
 
Old October 26th, 2009, 09:45 AM
Registered User
 
Join Date: Oct 2009
Posts: 5
Thanks: 7
Thanked 0 Times in 0 Posts
Default Following XSLT Logic not Working

hi All,

Thanks for quick response..

Actually I defined variable Dummy1 in one templete and after checking the condition i am assigning some value in to that.

and now after some processing i have to use the value of variable "dummy1" in to another templete not in same template where it defined and assigned.

and <XSL:param> is not allowing inside <xsl:IF>
so <xsl:call-templete> and <xsl:with-parm> is not applicable here.

so please suggest some other way...

Regards,
Sanket
[email protected]
Cognizant Tech Solutions, India.

Last edited by sanket002; October 26th, 2009 at 09:48 AM..
 
Old October 26th, 2009, 10:23 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

It's easier to tell you what you did wrong if you show us what you did.

To pass parameters to templates, you must

(a) declare the template parameter in the template being called:

Code:
<xsl:template name="x">
  <xsl:param name="p"/>
  ...
(b) pass the parameter in the call-template

Code:
 <xsl:call-template name="x">
   <xsl:with-param name="p" select="$dummy1"/>
 </xsl:call-template>
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
The Following User Says Thank You to mhkay For This Useful Post:
sanket002 (October 27th, 2009)
 
Old October 27th, 2009, 01:05 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 291
Thanks: 9
Thanked 29 Times in 29 Posts
Thumbs up

With mhkay's above idea, try the below:<xsl:template match="/">
<xsl:variable name="dummy" select="ArrayOfBookMark/BookMark[ShortName='Asp-7041-EndVar1']/Value"/>
<xsl:variable name="dummy1">
<xsl:choose>
<xsl:when test="($dummy!='')">
<xsl:value-of select="$dummy"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$dummy"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:call-template name="new">
<xsl:with-param name="p" select="$dummy1"/>
</xsl:call-template>

</xsl:template><xsl:template name="new">
<xsl:param name="p"/>
<xsl:value-of select="$p"/>
</xsl:template>
__________________
Rummy
The Following User Says Thank You to mrame For This Useful Post:
sanket002 (October 27th, 2009)
 
Old October 27th, 2009, 06:37 AM
Registered User
 
Join Date: Oct 2009
Posts: 5
Thanks: 7
Thanked 0 Times in 0 Posts
Default Following XSLT Logic not Working

Hi all,
Thanks for Response,
Here is the code:

First Templete where i m checking "dummy" and assigning value in to "dummy1"
<xsl:template name="default7004-3">
<xsl:variable name="dummy" select="ArrayOfBookMark/BookMark[ShortName='NDO-7004-EndVar3']/Value"/>
<xsl:choose>
<xsl:when test="($dummy!='')">
<xsl:variable name="dummy1" select="$dummy"/>
<xsl:value-of select="$dummy1"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="dummy1" select="'$'"/>
<xsl:value-of select="$dummy1"/>
</xsl:otherwise>
</xsl:choose>
............
...........
...........
</xsl:template>


Second Templete:

<xsl:templete name="dvalueadded">
.........

..........
..........
and here i want only the value of second variable.."dummy1" of the first templete.

</xsl:templete>


let me know still i m missing something........

Regards
Sanket.

Last edited by sanket002; October 27th, 2009 at 07:36 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Valid Javascript to get working in xslt ismailc XSLT 2 January 7th, 2009 05:05 AM
working with existing xml using xslt bv_arvind XSLT 1 March 12th, 2007 01:37 PM
Not able to implement the given logic in my XSLT ashyabhi_hp XSLT 2 January 27th, 2006 07:24 AM
XSLT Looping Logic - Very Urgent ujayaraman XSLT 2 March 3rd, 2005 10:42 AM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM





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