Solved

If Nothing to add

Posted on 2006-06-21
8
209 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
  • 4
  • 4
8 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Author Comment

by:JasonWinn
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
> 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
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, …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now