|
next
|
 Subject: xsl:variable and Result Tree Fragments (no matches) Author: James Delaney Date: 17 Jul 2009 02:43 PM Originally Posted: 17 Jul 2009 02:05 PM
|
Hi all,
I'm creating a number of fragments to calculate various presence numbers. I'm creating variables from the results of these fragments then doing simple math to give the results.
I'm having an error when trying to add two variables (from the result-tree-fragment) when one variable has no value at all. In the source XML, the criteria returns no matches, so when the two variables are added, it returns NaN. I'm Trying to make a xsl:choose to determine the presence of a null fragment return, but can't seem to get the correct test attribute working.
Here's the XSLT 1.0code:
[code]
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="SophFemalePTNexusTC">
<xsl:for-each-group select="Census/Student
[(
(Gender='Female') and
(Classification='nexus') and
(@AdmitTerm='Fall Semester 2008') and
(TotCredits/@Load<8) and
(TotCredits/@TransferCredits>29.99) and
(TotCredits/@TransferCredits<60)
)]"
group-by="/">
<xsl:value-of select="count(current-group())"/>
</xsl:for-each-group>
</xsl:variable>
<xsl:variable name="SophFemalePTNexusUG">
<xsl:for-each-group select="Census/Student
[(
(Gender='Female') and
(Classification='nexus') and
(TotCredits/@Load<8) and
(TotCredits/@UG>29.99) and
(TotCredits/@UG<60)
)]"
group-by="/">
<xsl:value-of select="count(current-group())"/>
</xsl:for-each-group>
</xsl:variable>
<xsl:choose>
<xsl:when test="boolean($SophFemalePTNexusTC)='1'">
<xsl:value-of select="$SophFemalePTNexusUG + 0"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$SophFemalePTNexusUG + $SophFemalePTNexusTC"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I highlighted where the error is. In This example, there is no RTF for the variables $SophFemalePTNexusTC. For example, if I do a <Value-of select:$SophFemalePTNexusTC, it will return nothing.
The result of the above code picks the xsl:when, and not the xsl:otherwise as it should if properly testing the presence of a null RTF.
The boolean is the wrong process - as the RTF is not a number or a string (right?) it is not subject to a boolean, count, or number attribute? Any help would be greatly appreciated.
|
top
|
 Subject: xsl:variable and Result Tree Fragments (no matches) Author: James Delaney Date: 21 Jul 2009 06:50 PM Originally Posted: 21 Jul 2009 01:24 PM
|
Hi Alberto,
Your solution is the path I'm trying to work down, however my problem is also concerned with the fact that there's a likelihood that all variables will not be a number.
For example:
$FroshMaleFTNexusUG has no matches, therefore returns nothing (but could be NaN or a value)
$FroshMaleFTNexusTC has no matches, therefore returns nothing
$FroshMaleFTNexusUG - $FroshMaleFTNexusTC has no matches, therefore returns nothing
In this situation, which happens a lot, there is no value return. Currently, it's properly eliminating the possibility of trying to compute a NaN via the string test that is constructed.
However, what it's not doing is return a zero if any of the three above conditions return nothing at all. I tried to do if statements within these, but no good ;)
----- Trying to do it like this:
<xsl:choose>
<xsl:when test="string($FroshMaleFTNexusUG)=''">
<xsl:value-of select="$FroshMaleFTNexusTC"/>
</xsl:when>
<xsl:when test="string($FroshMaleFTNexusTC)=''">
<xsl:value-of select="$FroshMaleFTNexusUG"/>
</xsl:when>
<xsl:when test="string($FroshMaleFTNexusUG - $FroshMaleFTNexusTC)=''">
0
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$FroshMaleFTNexusUG - $FroshMaleFTNexusTC"/>
</xsl:otherwise>
</xsl:choose>
---------------------
Tried to do this to make every fragment return a number based on their NaN value, but it's always picking the 0 when (when it should be picking the otherwise
<xsl:variable name="FroshFemaleFTABDPUG">
<xsl:for-each-group select="Student
[(
(Gender='Female') and
(Classification='adult') and
(TotCredits/@Load>11.99) and
(TotCredits/@UG<30)
)]"
group-by="/">
<xsl:choose>
<xsl:when test="string(count(current-group()=''))">
<xsl:text>0</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="count(current-group())"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:variable>
<xsl:value-of select="$FroshFemaleFTABDPUG"/><br/>
However, as all when's are true, all three are getting printed blank. The zero is not being forced out when both variables are empty.
Your help has been critical. Thank you again.
Best, James
>Hi James,
>the string($FroshMaleFTNexusUG
>- $FroshMaleFTNexusTC) will
>return NaN if either one
>variable is not a number, so
>you are printing at least one
>empty string by using two
>xsl:value-of.
>If you are building the two
>variables in a way that you
>know they are either an empty
>string or a valid number, try
>using this code
>
><xsl:when
>test="string($FroshMaleFTNexus
>UG)=''">
><xsl:value-of
>select="$FroshMaleFTNexusTC"/&
>gt;
> </xsl:when>
><xsl:when
>test="string($FroshMaleFTNexus
>TC)=''">
><xsl:value-of
>select="$FroshMaleFTNexusUG"/&
>gt;
> </xsl:when>
> <xsl:otherwise>
><xsl:value-of
>select="$FroshMaleFTNexusUG -
>$FroshMaleFTNexusTC"/>
> </xsl:otherwise>
>
>Alberto
>
|
|
|
|