Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Conditional Variables in XSL 1

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Ok... I recently had this dilema, and finally figured it out, so I decided to make it a tip in case anyone else had the same problem...

I have this form that shows contents and makes calculations based on the XML content, via an XSL doc...

To cut to the chase...

I set up Variables to handle the calculations...

The basic calculations were something this:
Code:
$Material = "QTY * MatlCost * MatlLength"
$Spring   = "QTY * SprgCost * SprgQTY"
$Adapter  = "QTY * AdptCost * AdptQTY"
$Machine  = "QTY * MachCost * (MachIDLen + MachODLen)"
$Total    = "$Material + $Spring + $Adapter + $Machine"

Now the problem was, if there was no Adapter, the AdptCost & AdptQTY would return NaN (Not a Number) which would cause the $Adapter to return NaN, which would cause the $Total to return NaN...

So obviously, this was a problem...

I needed a way to set the Variable to 0 if the Element did not exist...

The way I was displaying the line items was such as this:
Code:
<tr>
  <td>Description</td>
  <td>QTY</td>
  <td>Rate</td>
  <td>Length</td>
  <td>Total</td>
</tr>
[b]<xsl:if test="MatlCost">[/b]
  <tr>
    <td>Material</td>
    <td><xsl:value-of select="QTY" /></td>
    <td><xsl:value-of select="MatlCost" /></td>
    <td><xsl:value-of select="MatlLength" /></td>
    <td><xsl:value-of select="$Material" /></td>
  </tr>
[b]</xsl:if>[/b]
[b]<xsl:if test="SprgCost">[/b]
  <tr>
    <td>Spring</td>
    <td><xsl:value-of select="QTY * SprgQTY" /></td>
    <td><xsl:value-of select="SprgCost" /></td>
    <td></td>
    <td><xsl:value-of select="$Spring" /></td>
  </tr>
[b]</xsl:if>[/b]
[b]<xsl:if test="AdptCost">[/b]
  <tr>
    <td>Adapter</td>
    <td><xsl:value-of select="QTY * AdptQTY" /></td>
    <td><xsl:value-of select="AdptCost" /></td>
    <td></td>
    <td><xsl:value-of select="$Adapter" /></td>
  </tr>
[b]</xsl:if>[/b]
[b]<xsl:if test="MachCost">[/b]
  <tr>
    <td>Adapter</td>
    <td><xsl:value-of select="QTY" /></td>
    <td><xsl:value-of select="MachCost" /></td>
    <td><xsl:value-of select="MachIDLen + MachODLen" /></td>
    <td><xsl:value-of select="$Machine" /></td>
  </tr>
[b]</xsl:if>[/b]
<tr>
  <td colspan="4">Grand Total</td>
  <td><xsl:value-of select="$Total" /></td>
</tr>

So I tried this at first...
Code:
<xsl:if test="AdptCost">
  [b]<xsl:variable name="Adapter" select="QTY * AdptCost * AdptQTY" />[/b]
</xsl:if>
<xsl:if test="not(AdptCost)">
  [b]<xsl:variable name="Adapter" select="0" />[/b]
</xsl:if>
Which did not work...

I then tried <xsl:choose> as well...

But... It turns out that if you define the variable in an IF or CHOOSE block, it is limited to the scope within the block you defined it in...

then it dawned on me that there were 2 ways to assign variables...
Code:
<xsl:variable name="Adapter" [b]select="X"[/b] />
or
Code:
<xsl:variable name="Adapter">
  [b]<xsl:value-of select="X" />[/b]
</xsl:variable>

so I decided to invert it, and try this:
Code:
[COLOR=blue][b]<xsl:variable name="Adapter">
  <xsl:if test="AdptCost">
    <xsl:value-of select="QTY * AdptCost * AdptQTY">
  </xsl:if>
  <xsl:if test="not(AdptCost)">
    <xsl:value-of select="0">
  </xsl:if>
</xsl:variable>[/b][/color]
Which turned out to work great...

So there you have it... that is how to create conditional variables, and there are multiple uses for them, so get creative...

You can use <xsl:choose> in place of the <xsl:if> blocks with this if you have multiple conditions...

You could also combine this with a call template, to shorten the code in some cases, especially if there are multiple variables, with common patterns...

So, I hope this helps someone out there...
Good Luck ;-)
-Josh

Visit My Site
PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top