Solved

Error in XPath 2.0 expression

Posted on 2013-05-22
4
545 Views
Last Modified: 2013-05-23
I switched my XSL from XSLT 1.0 to 2.0 because I wanted to be able to access some of the functions in 2.0, and now I'm getting the following error message:

Error in XPath 2.0 expression, Cast failed, invalid lexical value - xs:double ''

My XSL looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions">
	<xsl:variable name="SQM_Petroleum_Refining_CH4_Emission_Quantity">
		<xsl:choose>
			<xsl:when test="/Report/Report_Detail/Sources/SQM/Petroleum_Refining/Emissions/Emission[Gas = 'CH4' ]/Emission_Quantity  != '' ">
				<xsl:value-of select="number(sum(/Report/Report_Detail/Sources/SQM/Petroleum_Refining/Emissions/Emission[Gas = 'CH4' ]/Emission_Quantity))"/>
			</xsl:when>
			<xsl:otherwise><xsl:value-of select="number(0)"/></xsl:otherwise>
		</xsl:choose>
	</xsl:variable>
	<xsl:template match="/">
	<xsl:value-of select="$SQM_Petroleum_Refining_CH4_Emission_Quantity"/>
	</xsl:template>
</xsl:stylesheet>

Open in new window


My XML looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Test.xsl"?>
<Report>
	<Report_Detail>
		<Sources>
			<SQM>
				<Petroleum_Refining>
					<SectionTitle>(a) catalyst regeneration</SectionTitle>
					<Emissions>
						<Emission>
							<Gas>CH4</Gas>
							<Not_Applicable_Indicator>0</Not_Applicable_Indicator>
							<Methodology>ON.203(a)(2)</Methodology>
							<Emission_Quantity>0.0006</Emission_Quantity>
							<Emission_Calculated_Quantity>0.0126</Emission_Calculated_Quantity>
						</Emission>
					</Emissions>
					<SectionTitle>(b) process vents</SectionTitle>
					<Emissions>
						<Emission>
							<Gas>CH4</Gas>
							<Not_Applicable_Indicator>1</Not_Applicable_Indicator>
							<Methodology/>
							<Emission_Quantity/>
							<Emission_Calculated_Quantity>0.0000</Emission_Calculated_Quantity>
						</Emission>
					</Emissions>
					<SectionTitle>(e) flares and other control devices</SectionTitle>
					<Emissions>
						<Emission>
							<Gas>CH4</Gas>
							<Not_Applicable_Indicator>0</Not_Applicable_Indicator>
							<Methodology>ON.203(e)</Methodology>
							<Emission_Quantity>5.1580</Emission_Quantity>
							<Emission_Calculated_Quantity>108.3180</Emission_Calculated_Quantity>
						</Emission>
					</Emissions>
				</Petroleum_Refining>
			</SQM>
		</Sources>
	</Report_Detail>
</Report>

Open in new window

0
Comment
Question by:mariita
  • 3
4 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39189418
XSLT2 is type sensitive.

You need to cast to number and filter out the empty ones

            <xsl:when test="/Report/Report_Detail/Sources/SQM/Petroleum_Refining/Emissions/Emission[Gas = 'CH4' ]/Emission_Quantity[normalize-space()]">
                <xsl:value-of select="sum(/Report/Report_Detail/Sources/SQM/Petroleum_Refining/Emissions/Emission[Gas = 'CH4' ]/Emission_Quantity[normalize-space()]/number())"/>
            </xsl:when>
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 39189421
but you don't need the choose construct,
the sum of no nodes is 0
so take the ones that are castable as xs:double directly

    <xsl:variable name="SQM_Petroleum_Refining_CH4_Emission_Quantity"
        select="sum(/Report/Report_Detail/Sources/SQM/Petroleum_Refining/Emissions/Emission[Gas = 'CH4' ]/Emission_Quantity[. castable as xs:double])"/>
0
 

Author Closing Comment

by:mariita
ID: 39190287
Thanks again.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39190484
welcome
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse <assert> tag in XSD1.1 using XSOM Parser? 4 68
C# Modify Read / Write / Update / Delete to  XML 2 66
MS SQL Database Generating XML using query 21 44
XSLT XML 4 19
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…
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…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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