Using vs functions using templates in xslt?

What are the pros and cons of functions versus templates in xslt?

I want to send unix-timestamp and get an answer like "today" or "tomorrow" or "next week". Which method is most suitable for this?

+3
source share
2 answers

The main reason for choosing <xsl:function>a named template is a much higher degree of function functionality.

It is very easy and convenient to write <xsl:function>, which displays the desired results :

 <xsl:function name="my:when" as="xs:string">
  <xsl:param name="pDateTime" as="xs:dateTime"/>

  <xsl:sequence select=
  "for $vToday in xs:dateTime(current-date()),
       $vTomorrow in $vToday
                    + xs:dayTimeDuration('P1D'),
       $vDayAfterTomorrow in $vTomorrow
                    + xs:dayTimeDuration('P1D'),
       $vNextWeek in $vToday
                    + 7* xs:dayTimeDuration('P1D'),
       $vNextFortnight in $vNextWeek
                    + 7* xs:dayTimeDuration('P1D')

       return
         if($pDateTime lt $vToday)
           then 'in the Past'
           else if($pDateTime lt $vTomorrow)
             then 'Today'
             else if($pDateTime lt $vDayAfterTomorrow)
              then 'Tomorrow'
             else if($pDateTime lt $vNextWeek)
              then 'This week'
             else if($pDateTime lt $vNextFortnight)
              then 'Next week'
              else 'In the Future'
  "/>
 </xsl:function>

Here is the complete conversion :

<xsl:stylesheet version="2.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:xs="http://www.w3.org/2001/XMLSchema"
 xmlns:my="my:my">
 <xsl:output omit-xml-declaration="yes"/>

 <xsl:template match="/">
  <xsl:sequence select="my:when(current-dateTime())"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P1D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P2D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P3D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P4D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P5D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P6D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P7D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P8D'))"/>,
  <xsl:sequence select="my:when(current-dateTime()
                               +xs:dayTimeDuration('P9D'))"/>
 </xsl:template>

 <xsl:function name="my:when" as="xs:string">
  <xsl:param name="pDateTime" as="xs:dateTime"/>

  <xsl:sequence select=
  "for $vToday in xs:dateTime(current-date()),
       $vTomorrow in $vToday
                    + xs:dayTimeDuration('P1D'),
       $vDayAfterTomorrow in $vTomorrow
                    + xs:dayTimeDuration('P1D'),
       $vNextWeek in $vToday
                    + 7* xs:dayTimeDuration('P1D'),
       $vNextFortnight in $vNextWeek
                    + 7* xs:dayTimeDuration('P1D')

       return
         if($pDateTime lt $vToday)
           then 'in the Past'
           else if($pDateTime lt $vTomorrow)
             then 'Today'
             else if($pDateTime lt $vDayAfterTomorrow)
              then 'Tomorrow'
             else if($pDateTime lt $vNextWeek)
              then 'This week'
             else if($pDateTime lt $vNextFortnight)
              then 'Next week'
              else 'In the Future'
  "/>
 </xsl:function>
</xsl:stylesheet>

, ( - ), , :

  Today,
  Tomorrow,
  This week,
  This week,
  This week,
  This week,
  This week,
  Next week,
  Next week,
  Next week
+1

.

XSLT , .

+1

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


All Articles