Solved

If Nothing to add

Posted on 2006-06-21
8
225 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
Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

726 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