Solved

If Nothing to add

Posted on 2006-06-21
8
214 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

The Problem How to write an Xquery that works like a SQL outer join, providing placeholders for absent data on the outer side?  I give a bit more background at the end. The situation expressed as relational data Let’s work through this.  I’ve …
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. 
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

785 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