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 5th, 2004, 03:25 AM
Authorized User
 
Join Date: Sep 2004
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conditional processing in XSL

I have a requirement where I need to transform some XML into a different format. During this conversion I need to perform some calculations on some elements and return the calculated value into a new element in my transformed xml.

The problem is that some elements may not occur in my source xml and if they do not occur then I need to do the processing without them.

Here is an example;

I could have a car to finance with the following elements.
<CASHPRICE>12000.00</CASHPRICE>
<DEPOSIT>100.00</DEPOSIT>
<PARTEXCHANGE>100.00</PARTEXCHANGE>
<SETTLEMENTAMOUNT>100.00</SETTLEMENTAMOUNT>

I need to workout the effective deposit as a percentage of the cash price in my new xml.
<DEPOSITPERCENTAGE> = (DEPOSIT + PARTEXCHANGE - SETTLEMENTAMOUNT) / CASHPRICE

The deposit, partexchange and settlementamount elements may not always appear in the source xml and I need a way of performing the calculation conditionally if these elements do not exist.

I thought a series of for-each select statements and then <xsl:when test="DEPOSIT[. != 0] and PARTEXCHANGE[. != 0] and SETTLEMENTAMOUNT[. != 0]">

Does anyone have any other ideas how to approach this problem?

I do see one problem that might occur where if either some / all of these conditions are true then you would get the calucation being fired more than once.

Thanks for any pointers.

 
Old October 5th, 2004, 03:43 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Declare variables whose value is initialized to that of the element if it is present, or the default value otherwise; then perform the calculation on the variables.

In 2.0 this is easy:

<xsl:variable name="discount" select="(DISCOUNT, 0)[1]"/>

In 1.0 it's a bit more convoluted:

<xsl:variable name="discount">
 <xsl:choose>
 <xsl:when test="DISCOUNT"><xsl:value-of select="DISCOUNT"/></xsl:when>
 <xsl:otherwise>0</xsl:otherwise>
</xsl:variable>

or you can cheat if you want (but it's horrible):

<xsl:variable name="discount" select="number(concat('0', DISCOUNT))"/>



Michael Kay
http://www.saxonica.com/
 
Old March 18th, 2008, 12:04 AM
Registered User
 
Join Date: Feb 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, this was handy for me as well. You did forget to close the choose condition though.

Shane C





Similar Threads
Thread Thread Starter Forum Replies Last Post
conditional xslt processing based on java array twilson997 XSLT 7 June 28th, 2006 07:32 AM
conditional xsl:import ? TPP XSLT 3 May 16th, 2006 05:18 AM
XSL processing question.. eladner XSLT 1 August 24th, 2004 07:58 AM
Create processing instruction in XSL hbcontract XML 6 May 7th, 2004 12:14 PM
XSL Transform with xsl string NOT xsl file skin XSLT 0 June 16th, 2003 07:30 AM





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