2

I am debugging an xslt stylesheet in Oxygen using the xalan processor. I cannot seem to get the current-time() or hours-from-time() functions to work. I get a "could not find function" error. What am i doing wrong? here is the code.

<xsl:variable name="isPm" select="hours-from-time(n1:TIME_REPORT) &gt;= 12"/>

1
  • Good question, +1. See my answer for an explanation and a solution for the current-time() function. Commented Jan 8, 2011 at 4:29

3 Answers 3

4

hours-from-time and current-time are XPath 2.0 functions. Xalan only supports XPath 1.0.

Later versions of Xalan support extension functions which will give some of this functionality. Both of the function you are looking for are there, in some form. Note that since XPath 1.0 doesn't understand date times you'll be dealing with strings. But see, for example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date">
    <xsl:template match="/">
        <html>
            <head>
                <title>Current Date Test</title>
            </head>
            <body>
                <h1>It's now <xsl:value-of select="date:date-time()"/>.</h1>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

See both the Xalan Extension Function page and also the EXSLT Extension Function page for Dates and Times.

Sign up to request clarification or add additional context in comments.

2 Comments

Is there any way to get the current time in xpath 1.0?
Nice one! This worked like a charm. I had already passed in the current hour in as a parameter as in Dimitre's solution below. Since this solution will also help me in the future if i need to use other unsupported XPath function i have marked it as the solution.
2
I am debugging an xslt stylesheet in Oxygen using the xalan processor. 

I cannot seem to get the current-time() or hours-from-time() functions to work. I get a "could not find function" error.

As noted by @lavinio and @Alejandro, these two functions were introduced in XPath 2.0, which is not supported by Xalan.

You may pass the current time as a parameter to the XSLT 1.0 transformation.

Do note, that even in XPath 2.0, multiple evaluations of the current-time() during a transformation, return the same value. So this function doesn't give you anything more than what you get by passing the current time as a parameter.

Comments

1

Those aren't standard XPath 1.0 functions. If your processor has those functions implemented as extensions, then you should add the correct namespace for them.

From http://www.w3.org/TR/xslt#section-Extension-Functions

If a FunctionName in a FunctionCall expression is not an NCName (i.e. if it contains a colon), then it is treated as a call to an extension function. The FunctionName is expanded to a name using the namespace declarations from the evaluation context.

If the XSLT processor does not have an implementation of an extension function of a particular name available, then the function-available function must return false for that name. If such an extension function occurs in an expression and the extension function is actually called, the XSLT processor must signal an error. An XSLT processor must not signal an error merely because an expression contains an extension function for which no implementation is available.

If the XSLT processor has an implementation of an extension function of a particular name available, then the function-available function must return true for that name. If such an extension is called, then the XSLT processor must call the implementation passing it the function call arguments; the result returned by the implementation is returned as the result of the function call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.