ani
ani

Reputation: 21

Getting error "XPath is invalid" for "hours-from-duration($duration)"

I need help with one of my issues. I am new to XSLT. Right now I am trying to write an XSLT which will generate text output (example: "01:30"). In my XSLT 2.0, I am calling the XPath function hours-from-duration($duration) and this function is throwing the error

XPath is invalid

I can also see the above error in the following logs. Please help me with my issues. Thanks...

16:01:39,403 ERROR [main] JAXPSAXProcessorInvoker - Error checking type of the expression 'funcall(hours-from-duration, [variable-ref(duration/node-set)])'.

16:01:39,404 ERROR [main] JAXPSAXProcessorInvoker - Could not compile stylesheet

javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:858) at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTransformer(TransformerFactoryImpl.java:648)

My XSLT:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times" 
    xmlns:str="http://exslt.org/strings" 
    xmlns:fn="http://www.w3.org/2005/xpath-functions"
    version="1.0" xmlns:xalan="http://xml.apache.org/xslt">
    <xsl:output method="text" />
    <xsl:variable name="request" select="/*[local-name()='Payout']/*[local-name()='Request']" />
    <xsl:variable name="duration" select="$request/Time" />
        
    <xsl:template match="/">
        <xsl:value-of select="(hours-from-duration($duration))"/>
        <xsl:text>:</xsl:text>
        <xsl:value-of select="(minutes-from-duration($duration))"/>
    </xsl:template>
</xsl:stylesheet>

XML Input:

<Payout>
 <Request Commit="true" Transaction="false">
  <Month>JAN</Month>
  <Time>P01H30M33S</Time>
 </Request>
</Payout>

Upvotes: 2

Views: 2254

Answers (2)

Michael Kay
Michael Kay

Reputation: 163342

hours-from-duration() is an XPath 2.0 function. You are using Xalan, which only supports XSLT 1.0 and XPath 1.0.

Furthermore, these functions expect an object of type xs:duration. You are passing it a node (a Time element). If you switch to an XSLT 2.0 processor, you will need either (a) to make sure the processor is schema-aware and Time is validated as an xs:duration, or (b) to convert it to type xs:duration explicitly, by calling xs:duration(Time).

And of course you will need to make sure it's a valid duration as pointed out by @MadsHansen

Upvotes: 1

Mads Hansen
Mads Hansen

Reputation: 66723

Your duration value is invalid. It is missing a "T". It should be PT01H30M33S.

Upvotes: 1

Related Questions