2010-07-02 66 views
0

是具有以下結構如何從一個XML文件中讀取日期和使用XSL解析它

<?xml version="1.0" encoding="utf-8"?> 
<?xml-stylesheet type="text/xsl" href="TestFile.xsl"?> 
<RootElement> 
    <Date FileModified="7/2/2010 12:54:53 PM" /> 
    <Child Name="A"/> 
    <Child Name="B"/> 
    <Child Name="C"/> 
    <Child Name="D"/> 
    <Child Name="E"/> 
</RootElement> 

我需要讀取該文件的日期屬性值,並傳遞到

var d=new Date( date); 
的xml文件

這裏是我的XSL文件

<?xml version="1.0" encoding="ISO-8859-1"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:template match="/"> 
<html> 
<head></head> 
    <body> 
    <center> 
     <b> 
     <script type="text/javascript"> 
      var d_names = new Array("Sunday", "Monday", "Tuesday","Wednesday", "Thursday", "Friday", "Saturday"); 
      var m_names = new Array("January", "February", "March","April", "May", "June", "July", "August","September","October","November", "December"); 

      var d = new Date(-----------); // here i need to get the date from the xsl file 

      var curr_day = d.getDay(); 
      var curr_date = d.getDate(); 
      var sup = ""; 
      if (curr_date == 1 || curr_date == 21 || curr_date ==31) 
      { 
      sup = "st"; 
      } 
      else if (curr_date == 2 || curr_date == 22) 
      { 
     sup = "nd"; 
     } 
      else if (curr_date == 3 || curr_date == 23) 
      { 
      sup = "rd"; 
      } 
      else 
      { 
      sup = "th"; 
      } 
      var curr_month = d.getMonth(); 
      var curr_year = d.getFullYear(); 
      var curr_hour=d.getHours(); 
      var curr_period="AM" 
      if(curr_hour>12) 
      { 
      curr_hour=curr_hour-12; 
      curr_period="PM" 
      } 
      document.write(d_names[curr_day]+ "&#160;&#160;&#160;&#160;&#160;" +curr_date + "<sup>"+ sup + " </sup> " + m_names[curr_month] + "&#160;&#160;" + curr_year+ "&#160;&#160;&#160;&#160;&#160;&#160;"+curr_hour+" : "+d.getMinutes()+" : "+d.getSeconds()+" "+curr_period); 
     </script> 
     </b> 
     <xsl:choose> 
     <xsl:when test="//Child"> 
      <br/> 
      <br/> 
      <br/> 
      <b>SampleTable</b> 
      <br/> 
      <br/> 
      <table border="1"> 
      <tr bgcolor="RGB(0,0,127)" > 
       <th width="5"> 
       <font color="white">S.No</font> 
       </th> 
       <th width="250"> 
       <font color="white"> Name</font> 
       </th> 
      </tr> 
      <xsl:for-each select ="//Child"> 
       <tr> 
       <td> 
        <xsl:number value="position()" format="01"/> 
       </td> 
       <td> 
        <xsl:value-of select="@Name"/> 
       </td> 
       </tr> 
      </xsl:for-each> 
      </table> 
     </xsl:when > 
     <xsl:otherwise ></xsl:otherwise> 
     </xsl:choose > 
    </center> 
    </body > 
</html> 
</xsl:template> 
</xsl:stylesheet> 

有沒有辦法做到這一點

回答

1
var d = new Date('<xsl:value-of select="RootElement/Date/@FileModified"/>') 
0

此外,您還可以得到德慾望結果用純XSLT,像這樣:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:variable name="m_names"> 
     <January/><February/><March/><April/><May/><June/><July/><August/><September/><October/><November/><December/> 
    </xsl:variable> 
    <xsl:variable name="d_names"> 
     <Sunday/><Monday/><Tuesday/><Wednesday/><Thursday/><Friday/><Saturday/> 
    </xsl:variable> 
    <xsl:variable name="modules" select="'033614625035034025036146'"/> 
    <xsl:variable name="date" select="/*/*/@FileModified"/> 
    <xsl:variable name="day" select="substring-before($date,'/')"/> 
    <xsl:variable name="month" select="substring-before(substring-after($date,'/'),'/')"/> 
    <xsl:variable name="year" select="substring($date,7 + string-length($date) - 22,4)"/> 
    <xsl:variable name="calc-day" select="(($year - 1) mod 7 + 
             (floor(($year - 1) div 4) - 3 * floor((floor(($year - 1) div 100) + 1) div 4)) mod 7 + 
             substring($modules, 
               12 * ($year mod 4 = 0 and $year mod 100 != 0) + $month, 
               1) + 
             $day mod 7) 
             mod 7 + 1"/> 
    <xsl:template match="/"> 
     <html> 
      <head></head> 
      <body> 
       <center> 
        <b> 
         <xsl:value-of select="concat(
                name(document('')/*/xsl:variable[@name='d_names']/*[$calc-day]), 
                '&#160;&#160;&#160;&#160;&#160;', 
                $day 
                )"/> 
         <sup> 
          <xsl:choose> 
           <xsl:when test="$day = 1 or $day = 21">st</xsl:when> 
           <xsl:when test="$day = 2 or $day = 22">nd</xsl:when> 
           <xsl:when test="$day = 3 or $day = 23">rd</xsl:when> 
           <xsl:otherwise>th</xsl:otherwise> 
          </xsl:choose> 
         </sup> 
         <xsl:value-of select="concat(
                '&#160;&#160;&#160;&#160;&#160;', 
                name(document('')/*/xsl:variable[@name='m_names']/*[number($month)]), 
                '&#160;&#160;', 
                $year, 
                '&#160;&#160;&#160;&#160;&#160;', 
                substring($date,12 + string-length($date) - 22,2), 
                ' : ', 
                substring($date,15 + string-length($date) - 22,2), 
                ' : ', 
                substring($date,18 + string-length($date) - 22,2), 
                ' ', 
                substring($date,string-length($date)-1) 
                )"/> 
        </b> 
        <xsl:apply-templates/> 
       </center> 
      </body > 
     </html> 
    </xsl:template> 
    <xsl:template match="Child[1]"> 
     <br/> 
     <br/> 
     <br/> 
     <b>SampleTable</b> 
     <br/> 
     <br/> 
     <table border="1"> 
      <tr bgcolor="RGB(0,0,127)" > 
       <th width="5"> 
        <font color="white">S.No</font> 
       </th> 
       <th width="250"> 
        <font color="white"> Name</font> 
       </th> 
      </tr> 
      <xsl:apply-templates select="../Child" mode="child"/> 
     </table> 
    </xsl:template> 
    <xsl:template match="Child" mode="child"> 
     <tr> 
      <td> 
       <xsl:number value="position()" format="01"/> 
      </td> 
      <td> 
       <xsl:value-of select="@Name"/> 
      </td> 
     </tr> 
    </xsl:template> 
</xsl:stylesheet> 

注意:如果你使用一個更好的日期格式提取可能是更簡潔。

+0

u能請講解使用 的 元件 – 2010-07-07 04:00:03

+0

@Pramodh:我使用的方法來計算星期幾描述HTTP:/ /es.wikipedia.org/wiki/Algoritmo_para_calcular_el_d%C3%ADa_de_la_semana。如果你願意,你可以選擇別人。所以,'$ modules'按月保留一些偏移量(正常年和閏年)。 – 2010-07-07 13:23:23