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 June 18th, 2007, 07:10 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Both "when" hit in choose

I need to get a discount value. The problem is that every article has a discountGroup, but some of them has another discount based on the article number. if it's a discont on the article number, I should use that and not the group. They use the same field "Id".

In my for-each I do a choose, and i really think it's correkt. But it seems like the second test always returns true "<xsl:when test="$ArticleId != @Id" > " And i can't understand why, here is some test data:

<Article Id="2030029" DiscountId="RA">
<Article Id="2030039" DiscountId="RA">
<Article Id="2031292" DiscountId="XE">

<Discount Id="2030029" DiscountValue="100.000000" />
<Discount Id="RA" DiscountValue="10" />
<Discount Id="XE" DiscountValue="5" />

Article 2030029 will now return 100.00000010 instead of 100.000000 (variable Sum)

and here is my xslt:

<xsl:template name="GetNetprice">
<xsl:param name="ArticleId" />
<xsl:param name="DiscountId" />
<xsl:param name="GrossPrice" />


<xsl:variable name="Sum">
<xsl:for-each select="//*[local-name()='Discount'] ">
<xsl:choose>
<xsl:when test="$ArticleId = @Id" >
<xsl:value-of select="@DiscountValue"/>
</xsl:when>
<xsl:when test="$ArticleId != @Id" >
<xsl:if test="$DiscountId = @Id" >
<xsl:variable name="Discount" select="(@GrossPrice * @DiscountValue) div 100" />
<xsl:variable name="Net" select="@GrossPrice - $Discount" />
<xsl:value-of select="$Net"/>
</xsl:if>
</xsl:when>
</xsl:choose>
</xsl:for-each>
</xsl:variable>


<xsl:choose>
<xsl:when test="$Sum &gt; 0">
<xsl:element name="ns0:PRICE_AMOUNT">
<xsl:value-of select="$Sum" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="ns0:PRICE_AMOUNT">
<xsl:value-of select="@GrossPrice" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Thanx for help :)
 
Old June 18th, 2007, 08:02 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I suspect that some of your references to @GrossPrice should be to $GrossPrice, but I can't see how that accounts for the problem you are seeing. You haven't shown the call on the named template, perhaps there's an error there? Please try submitting a complete stylesheet and source document, so other people can see if they get the same result.

Your technique of looping through all the Discount elements and concatenating all those that match isn't a coding style I would recommend, it's intrinsically error-prone. You're only expecting one to match, so select it directly in a predicate:

<xsl:variable name="SpecificDiscount" select="//Discount[@Id=$ArticleId]/@DiscountValue"/>
<xsl:variable name="GenericDiscount" select="//Discount[@Id=$DiscountId]/@DiscountValue"/>
<xsl:variable name="Sum">
 <xsl:choose>
  <xsl:when test="$SpecificDiscount">
   <xsl:value-of select="$SpecificDiscount"/>
  <xsl:when>
  <xsl:when test="$GenericDiscount">
   <xsl:value-of select="$GrossPrice - ($GrossPrice * $GenericDiscount) div 100"/>
  </xsl:when
...


Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old June 18th, 2007, 09:02 AM
Registered User
 
Join Date: Jun 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you very much :) I'm quite new to XSLT, and stil thinks to much programing. I had to use this script in the Biztalk mapper.

The working script looks like this if someone should get a similar problem(Pretty much the same as mhKay posted):

<xsl:template name="GetNetprice">
<xsl:param name="ArticleId" />
<xsl:param name="DiscountId" />
<xsl:param name="GrossPrice" />
<xsl:variable name="SpecificDiscount" select="//*[local-name()='Discount'][@Id=$ArticleId]/@DiscountValue"/>
<xsl:variable name="GenericDiscount" select="//*[local-name()='Discount'][@Id=$DiscountId]/@DiscountValue"/>

<xsl:variable name="Sum">
 <xsl:choose>
  <xsl:when test="$SpecificDiscount">
   <xsl:value-of select="$SpecificDiscount"/>
  </xsl:when>
  <xsl:when test="$GenericDiscount">
   <xsl:value-of select="$GrossPrice - ($GrossPrice * $GenericDiscount) div 100"/>
  </xsl:when>
 </xsl:choose>
 </xsl:variable>

<xsl:choose>
<xsl:when test="$Sum &gt; 0">
<xsl:element name="ns0:PRICE_AMOUNT">
<xsl:value-of select="$Sum" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:element name="ns0:PRICE_AMOUNT">
<xsl:value-of select="$GrossPrice" />
</xsl:element>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

Thanks again, learned alot actually when I discovered I didn't have to loop :)






Similar Threads
Thread Thread Starter Forum Replies Last Post
choose between two directories and use one jjk2 PHP How-To 1 June 3rd, 2008 05:07 PM
Nested Choose Ryan Moore XSLT 6 November 9th, 2007 03:24 PM
Choose all by date? morpheus SQL Server ASP 3 July 30th, 2005 11:50 PM
xsl:choose djmarquette XSLT 5 March 29th, 2005 11:44 AM
"choose, when, otherwise" - problem natjimy XSLT 8 December 17th, 2004 02:22 PM





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