Using non-core EXSLT date functions with Xalan Java

I had EXSLT kernel date functions that worked well in some XSL templates that I have been using for many years. I would like to start using the new: seconds . This function is not one of the โ€œcoreโ€ functions, and therefore implementations may require compliance with EXSLT without it.

I am using Cocoon 2.1.11 with Xalan 2.7.1, which seems to have decided not to implement date:seconds .

Fortunately, the good people at EXSLT provide downloads to help you plug in individual features, but I can't figure out how to actually get the plugin.

I can use the basic functions using this template, for example:

 <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:date="http://exslt.org/dates-and-times" > <xsl:template match="/"> <xsl:value-of select="date:date-time()" /> </xsl:template> </xsl:stylesheet> 

When trying to use the date.seconds function date.seconds I tried the following:

  • Just add <xsl:import href="date.seconds.xsl" /> to my template and with date:seconds() . This causes the error message "The extension function could not find the org.apache.xalan.lib.ExsltDatetime.seconds method"
  • Removing xmlns:date as shown above. This causes me the error "Prefix must resolve namespace: date"
  • Placing the contents of date.seconds.xsl inside the template I'm trying to write, and then calling date:seconds() without an argument (by default it used the current time). I get this error message: "Calling the instance method to the second method requires an instance of the object as the first argument." Now it looks promising.
  • Adding an argument to date:seconds . I get an error: "The extension function could not find the java.lang.String.seconds ([ExpressionContext,]) methodโ€

Any suggestions for using this non-core EXSLT function?

Here is my current template that still tells me that it is trying to call java.lang.String.seconds() :

 <?xml version="1.0" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:func="http://exslt.org/functions" xmlns:date="http://exslt.org/dates and times"> <xsl:import href="date.seconds.xsl" /> <func:script language="exslt:javascript" implements-prefix="date" src="date.js"/> <func:script language="exslt:msxsl" implements-prefix="date" src="date.msxsl.xsl"/> <xsl:template match="/"> <xsl:value-of select="date:seconds('2014-02-27')" /> </xsl:template> </xsl:stylesheet> 
0
source share
2 answers

For those who want to collapse their own (especially for seconds() ), this can be done quite simply using this code:

 package tools; import java.text.SimpleDateFormat; import java.util.Date; import java.text.ParseException; public class Dates { /** * Converts a date in xs:dateTime format into seconds-since-the-epoch * * @param date The date to be converted, * in <code>s:dateTime</code> format (yyyy-MM-dd'T'hh:mm:ss). * * @return Number of seconds since <code>1970-01-01 00:00:00</code>, * or 0 if the date is blank or null. * @throws ParseException */ public static long seconds(String date) throws ParseException { if(null == date || 0 == date.trim().length()) return 0; SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss"); Date d = df.parse(date); return d.getTime() / 1000; } } 

This can be used through Apache Xalan as follows:

 <?xml ... xmlns:java="http://xml.apache.org/xalan/java" ... <xsl:variable name="some-date" select="...some xs:dateTime data ..." /> <xsl:value-of select="java:tools.Dates.seconds($some-date)" /> 

I will probably look at the source of Xalan to make it more flexible, but at the moment it seems to be doing what I would like to do.

+2
source

First, look: http://www.exslt.org/howto.html#other-implementations However, AFAIK Xalan does not support the func: script extension element (at least it says that itโ€™s not, which is not always same). OTOH, Xalan has its own extension mechanism - see: http://xml.apache.org/xalan-j/extensions.html

If it were me, I would just use a named template.

+1
source

Source: https://habr.com/ru/post/953802/


All Articles