XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
James DelaneySubject: 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&lt;8) and
(TotCredits/@TransferCredits&gt;29.99) and
(TotCredits/@TransferCredits&lt;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&lt;8) and
(TotCredits/@UG&gt;29.99) and
(TotCredits/@UG&lt;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.

Postnext
James DelaneySubject: xsl:variable and Result Tree Fragments (no matches)
Author: James Delaney
Date: 18 Jul 2009 02:26 AM
Think the following solution worked. It's giving me the proper when/otherwise outcomes that I would expect... however, I'm still suspicious. Is this a dangerous way or not a good way in testing a RTF?

<xsl:choose>
<xsl:when test="string($SophFemalePTNexusTC - $SophFemalePTNexusTC)='NaN'">
<xsl:value-of select="$SophFemalePTNexusUG"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$SophFemalePTNexusUG - $SophFemalePTNexusTC"/>
</xsl:otherwise>
</xsl:choose>

Postnext
Alberto MassariSubject: xsl:variable and Result Tree Fragments (no matches)
Author: Alberto Massari
Date: 20 Jul 2009 09:15 AM
Hi James,
these two tests are equivalent:

not(boolean(...))
string(...)='NaN'

Just keep in mind that boolean() invoked on a number returns 'false' if the number is 0 or NaN, while the string() function will only test for NaN.

Alberto

Postnext
James DelaneySubject: xsl:variable and Result Tree Fragments (no matches)
Author: James Delaney
Date: 20 Jul 2009 12:08 PM
>Hi James,
>these two tests are
>equivalent:
>
>not(boolean(...))
>string(...)='NaN'
>
>Just keep in mind that
>boolean() invoked on a number
>returns 'false' if the number
>is 0 or NaN, while the
>string() function will only
>test for NaN.
>
>Alberto


Alberto,

Again, thanks for your help with my newbie questions in the past weeks. The difference between those two returns will be useful to know!

Postnext
James DelaneySubject: xsl:variable and Result Tree Fragments (no matches)
Author: James Delaney
Date: 20 Jul 2009 06:05 PM
I'm having trouble fine tuning the following fragment:

<xsl:variable name="FroshMaleFTNexusTC">
<xsl:for-each-group select="Student
[(
(Gender='Male') and
(Classification='nexus') and
(TotCredits/@Load&gt;11.99) and
(TotCredits/@TransferCredits&gt;29.99)
)]"
group-by="/">
<xsl:value-of select="count(current-group())"/>
</xsl:for-each-group>
</xsl:variable>

<xsl:variable name="FroshMaleFTNexusUG">
<xsl:for-each-group select="Student
[(
(Gender='Male') and
(Classification='nexus') and
(TotCredits/@Load&gt;11.99) and
(TotCredits/@UG&lt;30)
)]"
group-by="/">
<xsl:value-of select="count(current-group())"/>
</xsl:for-each-group>
</xsl:variable>

<xsl:variable name="Frosh_Male_Nexus_FT">
<xsl:choose>
<xsl:when test="string($FroshMaleFTNexusUG - $FroshMaleFTNexusTC)='NaN' or 0">
<xsl:value-of select="$FroshMaleFTNexusTC"/>
<xsl:value-of select="$FroshMaleFTNexusUG"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$FroshMaleFTNexusUG - $FroshMaleFTNexusTC"/>
</xsl:otherwise>
</xsl:choose>
</xsl:variable>

<xsl:value-of select="$Frosh_Male_Nexus_FT"/>



The main issue, is that while the when/otherwise statements are now working, sometimes the value-of select is blank. Is there anyway I can test the following two value-of for no return and force in a Zero?

<xsl:when test="string($FroshMaleFTNexusUG - $FroshMaleFTNexusTC)='NaN' or 0">
<xsl:value-of select="$FroshMaleFTNexusTC"/>
<xsl:value-of select="$FroshMaleFTNexusUG"/>
</xsl:when>


Right now the when is selecting that part (as it should), but rather than return empty, I want it to return zero. Here's at least my take at it (broken code):

<xsl:when test="($FroshMaleFTNexusTC or FroshMaleFTNexusUG)=not(true())">
<xsl:text>0</xsl:text>
</xsl:when>

Postnext
Alberto MassariSubject: xsl:variable and Result Tree Fragments (no matches)
Author: Alberto Massari
Date: 21 Jul 2009 06:43 AM
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($FroshMaleFTNexusUG)=''">
<xsl:value-of select="$FroshMaleFTNexusTC"/>
</xsl:when>
<xsl:when test="string($FroshMaleFTNexusTC)=''">
<xsl:value-of select="$FroshMaleFTNexusUG"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$FroshMaleFTNexusUG - $FroshMaleFTNexusTC"/>
</xsl:otherwise>

Alberto

Posttop
James DelaneySubject: 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&gt;11.99) and
(TotCredits/@UG&lt;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
>

 
Topic Page 1 2 3 4 5 6 7 8 9 Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.