Can EXSLT date-and-time functions be used in XSLT 1.0 and processed using browser engine?
My goal: I need to transform a "date of birth" element in XML document to "age" value using XSL stylesheet and generate XHTML page. I am using the web browser (e.g. IE/FF) directly to open the XML document.
I know XSLT 2.0 has built-in dat开发者_C百科e and time functions, but I think no browser currently support this. So, I've been trying to use EXSLT functions instead without success.
Here are my sample test files:
test.xml
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<test>
</test>
test.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:date="http://exslt.org/dates-and-times">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:value-of select="date:date-time()"/>
</xsl:template>
</xsl:stylesheet>
Error on IE8:
Namespace 'http://exslt.org/dates-and-times' does not contain any functions.
Error on FF4:
Error during XSLT transformation: An unknown XPath extension function was called.
Does that mean EXSLT is not supported by major web browsers? Do I have to use XSLT proccessor like SAXON/Xalan? Am I doing something wrong? Is there an alternative way?
Use the EXSLT support matrix as a reference:
The following XSLT processors support date:date-time:
SAXON from Michael Kay (version 6.4.2)
Xalan-J from Apache (version 2.4.D1)
4XSLT, from 4Suite. (version 0.12.0a3)
libxslt from Daniel Veillard et al. (version 1.0.19)
libxslt
is used by Chrome, Opera and Safari, but date-time()
does not work since EXSLT is disabled:
I don't think it makes sense to add functions piecemeal; after nearly 5 years is there still anything preventing libexslt being included in the build and exsltRegisterAll() being called from registerXSLTExtensions() in XSLTExtensions.cpp?
IE uses MSXML
, which has the following support:
MSXML4 provided two great extension functions, ms:format-date() and ms:format-time() to aim at the latter problem, but they are not supported in .NET or MSXML3.
There is no ms:date-time()
function, but there is an MSXSL extension.
<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:ecma ="about:ecma">
<msxsl:script implements-prefix="ecma">
<![CDATA[
function GetCurrentDateTime()
{
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
return(month + "/" + day + "/" + year);
}
]]>
</msxsl:script>
<xsl:template match="/">
<xsl:value-of select="ecma:GetCurrentDateTime()"/>
</xsl:template>
</xsl:stylesheet>
Firefox uses Transformiix, which has support for EXSLT date-time()
since FF6.
References
- MDN: EXSLT
- EXSLT - date:date-time
- XSL Transformations (XSLT) in Mozilla
- Test Cases for XSLT support in browsers
- Mozilla Bug 603159 - implement exslt-date:date-time()
- Webkit Bug 4079 Support EXSLT with libexslt
- Mozilla Bug 265254 - support exlst:date
- Transformiix: Elements and Functions Available
- Building Practical Solutions with EXSLT.NET
- Microsoft XPath Extension Functions
精彩评论