2010-12-22 57 views
3

xslt中函數vs模板的優缺點是什麼?使用函數vs在xslt中使用模板?

我想發送一個unix時間戳並獲得像「今天」或「明天」或「下週」的答案。這種方法最合適嗎?

+0

如果您使用XSLT 1.0,您是否使用XSLT 1.0或2.0或者您有什麼樣的函數? – 2010-12-22 16:02:27

+0

好問題,+1。查看我的答案以獲得由提供的完整且簡單的解決方案。 – 2010-12-22 18:16:01

+0

xslt 1.0是我使用的一個! – Himmators 2010-12-23 00:57:01

回答

1

在命名模板上選擇<xsl:function>的主要原因是函數的可組合性要高得多。

這是非常容易和方便的編寫產生想要的結果一個<xsl:function>

<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 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最適合模式匹配和轉換,而不是計算。