2016-05-27 40 views
1

我有這個簡單的XML文件XSL如何找到特定日期的Julian日?

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="faan.xsl"?> 
<datum>2016-05-17</datum> 

我想使用XSL(XSL 2.0和擴展是不是一種選擇)來計算儒略日這個日期(正確答案是2457526,這只是練習!)並提出了這個似乎在許多地方複製粘貼的樣式表。 但是,當在資源管理器中打開文件時,它僅給出日期2016-05-17,當我在Excel中打開它時,它說我不能在此處使用xsl:with-param。 我明顯錯過了很簡單的事情,但是什麼?

<?xml version="1.0"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="xml" indent="yes"/> 

<xsl:template match="/"> 
    <xsl:for-each select="datum"> 
    <xsl:sort select="datum"/> 
      <dateline> 
       <datum><xsl:value-of select="datum"/></datum> 
      </dateline> 
    </xsl:for-each> 
</xsl:template> 

<xsl:template match="datum"> 
    <xsl:call-template name="calculate-julian-day" /> 
     <xsl:with-param name="year" select="substring(datum,1,4)" /> 
     <xsl:with-param name="month" select="substring(datum,6,2)" /> 
     <xsl:with-param name="day" select="substring(datum,9,2)" /> 
</xsl:template> 

<xsl:template name="calculate-julian-day"> 
    <xsl:param name="year"/> 
    <xsl:param name="month"/> 
    <xsl:param name="day"/> 

    <xsl:variable name="a" select="floor((14 - $month) div 12)"/> 
    <xsl:variable name="y" select="$year + 4800 - $a"/> 
    <xsl:variable name="m" select="$month + 12 * $a - 3"/> 

    <xsl:value-of select="$day + floor((153 * $m + 2) div 5) + $y * 365 + floor($y div 4) - floor ($y div 100) + floor($y div 400) - 32045"/> 
</xsl:template> 

</xsl:stylesheet> 
+0

你有基本的語法問題:你需要把'XSL:與 - **'xsl:call-template'裏面的參數**。而且'datum'的模板永遠不會被應用。 –

回答

0

這裏是一個可能的解決方案

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output omit-xml-declaration="yes" indent="yes"/> 


    <xsl:template match="datum"> 
    <xsl:call-template name="date-to-julian-day"> 
     <xsl:with-param name="date" select="."/> 
    </xsl:call-template> 
    </xsl:template> 

<!-- 
========================================================================== 
    Template: date-to-julian-day 
Description: Convert a date to julian day 
Parameters:- 
    <year> <month> <day> 
    or 
    <date> (format: yyyymmdd or yyyy-mm-dd) 
========================================================================== --> 
    <xsl:template name="date-to-julian-day"> 
     <xsl:param name="year"/> 
     <xsl:param name="month"/> 
     <xsl:param name="day"/> 
     <!-- or --> 
     <xsl:param name="date" select="''"/> 
     <!-- trim down date --> 
     <xsl:variable name="tdate" select="translate($date,'-','')"/> 
     <!-- decide which params were passed --> 
     <xsl:variable name="yyyy"> 
      <xsl:choose> 
      <xsl:when test="string-length($date) &gt; 0"> 
       <xsl:value-of select="substring($tdate,1,4)"/> 
      </xsl:when> 
      <xsl:otherwise><xsl:value-of select="$year"/></xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 
     <xsl:variable name="mm"> 
      <xsl:choose> 
       <xsl:when test="string-length($date) &gt; 0"> 
       <xsl:value-of select="substring($tdate,5,2)"/> 
       </xsl:when> 
       <xsl:otherwise><xsl:value-of select="$month"/></xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 
     <xsl:variable name="dd"> 
      <xsl:choose> 
       <xsl:when test="string-length($date) &gt; 0"> 
        <xsl:value-of select="substring($tdate,7,2)"/> 
       </xsl:when> 
       <xsl:otherwise><xsl:value-of select="$day"/></xsl:otherwise> 
      </xsl:choose> 
     </xsl:variable> 
     <!-- pre-calcs --> 
     <xsl:variable name="j0" select="ceiling(($mm - 14) div 12)"/> 
     <xsl:variable name="j1" select="floor((1461 * ($yyyy + 4800 + $j0)) div 4)"/> 
     <xsl:variable name="j2" select="floor((367 * ($mm - 2 - (12 * $j0))) div 12)"/> 
     <xsl:variable name="j3" 
         select="floor((3 * floor(($yyyy + 4900 + $j0) div 100)) div 4)"/> 
     <!-- final calc --> 
     <xsl:value-of select="$j1 + $j2 - $j3 + $dd - 32075"/> 
    </xsl:template> 

</xsl:stylesheet> 

當這個變換所提供的XML文檔施加:

<datum>2016-05-17</datum> 

有用,正確的結果產生

2457526 

署名

所提供的代碼是在datetime_lib.xsl XSLT 1.0庫的日期時間算術和轉換,通過我的朋友馬丁·羅林森寫模板的一部分 - XSelerator的作者 - 有史以來最好的XSLT IDE,它仍然是用戶友好且功能強大的用戶界面的典型例子。

有時在2010年左右的XSelerator是在sourceforge.net免費提供的,這是多麼讓人通常會獲得這些寶貴的模板庫保持..

+0

OP正在使用的(更短)模板沒有任何問題。他的問題在別處。 –

+0

向他展示更好的解決方案沒有錯。 –

+0

我很感激。我意識到我必須對XSL重複很多...... –