Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If Nothing to add

Posted on 2006-06-21
8
Medium Priority
?
232 Views
Last Modified: 2012-06-22
Hello!
I have my XSL form set to add up some values from an XML sheet. The XML sheet changes often because it is a report.

not all of the values are always present, hence if something is not present then I will probably get a NaN error.

Is there something I can do, if one of the things I am adding, is not present, that it just ignores it and moves on and adds the next xpath?

Let's say is goes to add 'overtime' but overtime is not present, can I just make it skip 'overtime' instead of giving me a NaN?


<xsl:value-of select="format-number(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value + /ReportData[1]/DataRow[RowData/@value = 'Prevail Mason OT*']/ColData[last()]/@value +
/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value +
/ReportData[1]/DataRow[RowData/@value = 'Double Time*']/ColData[last()]/@value, '###,###,##0.00')" />
0
Comment
Question by:JasonWinn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16954987
Hi JasonWinn,

I think I would put every term in a variable like this
<xsl:variable name="colLast">
    <xsl:choose>
        <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
        <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
    </xsl:choose>
</xsl:variable>

and add them like this
<xsl:value-of select="format-number($colLast + ... , '###,###,##0.00')" />

a bit less dense but a lot more clear

In XSLT2.0 you can have an if() then() else () for each term

Cheers!
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 16963357
Hey Gertone,
Thanks for the advice, I used that but maybe i messed up because I am getting a NaN thrown at me. The thing is, "ColLastMasonDT" wont even be in the report sometimes, but I think what you gave me works. Maybe i made a mistake?

                                    <xsl:variable name="colLastLabor">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMason">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMasonDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastLaborDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastDoubleTime">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Doubletime*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastOvertime">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:value-of select="format-number($colLastLabor + $colLastMason + $colLastMasonDT + $colLastLaborDT + $colLastDoubleTime + $colLastOvertime , '###,###,##0.00')" />
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 16963440
I just realised my "otherwise" statements were not correct, I fixed all of them and still getting a NAN

                                    <xsl:variable name="colLastLabor">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMason">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMasonDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason DT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastLaborDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor DT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastDoubleTime">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Double Time*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Double Time*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastOvertime">
                                          <xsl:choose>
                                                <xsl:when test="string(/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:value-of select="format-number($colLastLabor + $colLastMason + $colLastMasonDT + $colLastLaborDT + $colLastDoubleTime + $colLastOvertime , '###,###,##0.00')" />
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Author Comment

by:JasonWinn
ID: 16963504
Updated:

                                    <xsl:variable name="colLastLabor">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMason">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason OT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason OT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastMasonDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Mason DT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastLaborDT">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor DT*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Prevail Labor DT*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastDoubleTime">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Double Time*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Double Time*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:variable name="colLastOvertime">
                                          <xsl:choose>
                                                <xsl:when test="string(/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value) = 'NaN'">0</xsl:when>
                                                <xsl:otherwise><xsl:value-of select="/QBXML/QBXMLMsgsRs[1]/PayrollSummaryReportQueryRs[1]/ReportRet[1]/ReportData[1]/DataRow[RowData/@value = 'Overtime*']/ColData[last()]/@value"/></xsl:otherwise>
                                          </xsl:choose>
                                    </xsl:variable>
                                    
                                    <xsl:value-of select="format-number($colLastLabor + $colLastMason + $colLastMasonDT + $colLastLaborDT + $colLastDoubleTime + $colLastOvertime , '###,###,##0.00')" />
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16963719
I have a simplified example
You are still getting the NaN because one of the nodes is non existent

here is what you should do

<?xml version="1.0" encoding="UTF-8"?>
<tests>
  <a>0</a>
  <b>1</b>
  <c>2</c>
  <d>1</d>
</tests>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:variable name="eNode">
            <xsl:choose>
                <xsl:when test="not(//e) or string(//e) = 'NaN'">0</xsl:when>
                <xsl:otherwise><xsl:value-of select="//e"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="//a + //b + //c + //d + $eNode"/>
    </xsl:template>
</xsl:stylesheet>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 16963732
since you now have three times the same XPath evaluation,
itis even better to double the number of variables

like this
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:template match="/">
        <xsl:variable name="strENode" select="//e"/>
        <xsl:variable name="eNode">
            <xsl:choose>
                <xsl:when test="not($strENode) or string($strENode) = 'NaN'">0</xsl:when>
                <xsl:otherwise><xsl:value-of select="$strENode"/></xsl:otherwise>
            </xsl:choose>
        </xsl:variable>
        <xsl:value-of select="//a + //b + //c + //d + $eNode"/>
    </xsl:template>
</xsl:stylesheet>
0
 
LVL 3

Author Comment

by:JasonWinn
ID: 16963746
Gertone,
I will give that a shot and let you know in a couple minutes. Sometimes my node's will be non-existant though, does your simplied example fix that?

For instance, my value-of select= a + b + c + d could be there, but B may not always be in the report, or c may not always be in the report ect. Does your example take care of that, that it will just ignore non-existant nodes?
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 16963825
> Sometimes my node's will be non-existant though, does your simplied example fix that?

that was exactly the problem that cause you the NaN at the end
by testing the node existing with not($strENode) I make the variable 0 if the node is empty
note that in my example the <e> node is missing
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question