Using non-core EXSLT date functions with Xalan Java

前端 未结 2 1634
感情败类
感情败类 2021-01-28 17:07

I have had EXSLT\'s core date functions working well in some XSL templates I\'ve been using for years. I\'d like to start using a new one: seconds. This function is

相关标签:
2条回答
  • 2021-01-28 17:47

    For those who want to roll their own (for seconds() specifically), it can be done rather 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 like this:

    <?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'll probably look at the Xalan source in order to make this more flexible, but for now it seems to be doing what I'd like it to do.

    0 讨论(0)
  • 2021-01-28 18:06

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

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

    0 讨论(0)
提交回复
热议问题