?
Solved

If Nothing to add

Posted on 2006-06-21
8
Medium Priority
?
228 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
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. 
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

762 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