RE: [xsl] determine the number of payment methods and specify order depending on page type being viewed

Subject: RE: [xsl] determine the number of payment methods and specify order depending on page type being viewed
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Tue, 4 Nov 2008 09:31:17 -0600
Caroline,

Here's a first stab at narrowing down your problem.
- You have three <li> elements that are being output in any scenario, and only
the class of each is changing between them. So it sounds like you really just
want to:
  - push the switching-logic down into the class template
  - automate which <li> elements are created, based on the number of payment
methods specified in the input.
- Pulling the Direct Debit/Credit Card/Invoice parts out to some elements
global to the template might help reduce the amount of <xsl:choose> blocks
here.
- Also, you're not showing us where the checkOutProgressStep template is being
called, so we have to guess.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:html="http://www.w3.org/1999/xhtml"; xmlns:my="my-dummy-namespace"
exclude-result-prefixes="my">

...

<my:paymentMethod label="directDebit" order="1">Direct
Debit</my:paymentMethod>
<my:paymentMethod label="creditCard"  order="2">Credit
Card</my:paymentMethod>
<my:paymentMethod label="invoice"     order="3">Invoice</my:paymentMethod>
<xsl:variable name="paymentMethods" select="document('')//my:paymentMethod"
/>

<xsl:template name="checkOutProgressStep">
  <xsl:param name="label"/>
  <xsl:variable name="noOfNodes" select="count(/order/paymentMethods/*)" />
  <xsl:variable name="currentMethod" select="$paymentMethods[@label = $label]"
/>
  <xsl:for-each select="$paymentMethods[@order &lt;= $noOfNodes]">
    <li>
      <xsl:attribute name="class">
        <xsl:text>doubleLine</xsl:text>
        <xsl:if test="@order &lt; $currentMethod/@order"> completed</xsl:if>
        <xsl:if test="@order = $currentMethod/@order"> current</xsl:if>
      </xsl:attribute>
      <xsl:text>Payment by </xsl:text>
      <br />
      <xsl:value-of select="." />
    </li>
  </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Hope that helps.

~ Scott

-----Original Message-----
From: Caroline Umali [mailto:carolineumali@xxxxxxxxx]
Sent: Monday, November 03, 2008 6:09 PM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] determine the number of payment methods and specify order
depending on page type being viewed

Hi,

Sorry...first time in posting here...ok, so here's an amended version of
the XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
xmlns:html="http://www.w3.org/1999/xhtml";>
...
<xsl:template name="checkOutProgressStep">
   <xsl:param name="label"/>
   <xsl:variable name="noOfNodes" select="count(/order/paymentMethods/*)" />

   <xsl:choose>
       <xsl:when test="$noOfNodes = '3'"><!-- check for 3 elements -->
           <xsl:choose>
               <xsl:when test="$label = 'directDebit'">
                   <li class="doubleLine current">Payment by <br/>Direct
Debit</li>
                   <li class="doubleLine">Payment by <br/>Credit Card</li>
		   <li class="doubleLine">Payment by <br/>Invoice</li>
               </xsl:when>

               <xsl:when test="$label = 'creditCard'">
                   <li class="doubleLine completed">Payment by <br/>Direct
Debit</li>
                   <li class="doubleLine current">Payment by <br/>Credit
Card</li>
		   <li class="doubleLine">Payment by <br/>Invoice</li>
               </xsl:when>

               <xsl:when test="$label = 'invoice'">
                   <li class="doubleLine completed">Payment by <br/>Direct
Debit</li>
                   <li class="doubleLine completed">Payment by <br/>Credit
Card</li>
		   <li class="doubleLine current">Payment by <br/>Invoice</li>
               </xsl:when>
           </xsl:choose>
       </xsl:when>

   </xsl:choose>
</xsl:template>
...
</xsl:stylesheet>

You'll notice that for the current payment method (specified in the
xsl:when test="..."), the class "current" is added.  If a payment method
exists before the current one, then this will have a class "completed",
whereas any subsequent to the current matching payment will have no
extra class.

The method above is only for checking if all three payment types are
present.  I haven't even approached checking for 2 payment types or 1,
as I don't even know where to begin.

Apologies if this confusing, as I've been staring at it all day, and
appreciate any tips on bug fixing.

Current Thread