<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
					xmlns:content="http://purl.org/rss/1.0/modules/content/"
					xmlns:wfw="http://wellformedweb.org/CommentAPI/"
				  >
<channel>
<title><![CDATA[Saturn's Weblog - 分类：HTML/CSS/JavaScript]]></title>
<link>http://www.cnsaturn.com/category/web_basic</link>
<description><![CDATA[HTML/CSS/JavaScript]]></description>
<language>zh-CN</language>
<pubDate>Sat, 31 Jul 2010 16:59:04 -0400</pubDate>
<item>
<title><![CDATA[使用XSLT进行日期格式转换]]></title>
<link>http://www.cnsaturn.com/posts/converting-datetime-format-in-xslt</link>
<pubDate>Mon, 10 Aug 2009 10:53:00 -0400</pubDate>
<description><![CDATA[<p>
 现在有个问题需要用XSLT来实现，那就是在XSLT中如何将一个日期结点的格式由YYYY-MM-DD转换到DD/MM/YYYY？</p>
<p>
 我首先想到的就是<strong>利用XSLT中的带参数的模板，将此原始日期作为参数参数一个模板中，然后进行重新组合</strong>。说起来晕乎乎，还是直接进入实战吧。</p>
<p>
 假设现在有一个XML文档，内容如下：</p>
<pre class="brush:xml;">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;format_date.xsl&quot;?&gt;
&lt;notebooks&gt;
 &lt;notebook&gt;
  &lt;title&gt;红花还需绿叶衬，我是标题一&lt;/title&gt;
  &lt;date&gt;2009-08-11&lt;/date&gt;
 &lt;/notebook&gt;
 &lt;notebook&gt;
  &lt;title&gt;红花还需绿叶衬，我是标题二&lt;/title&gt;
  &lt;date&gt;2009-08-10&lt;/date&gt;
 &lt;/notebook&gt;
&lt;/notebooks&gt;</pre>
<p>
 在这个文档中，结点的日期为2009-08-11，即格式为YYYY-MM-DD，现在我们需要利用XSLT将其转换成DD/MM/YYYY，也就是11/08/2009。</p>
<p>
 关于实现思路，我在上面已经讲到，不再赘述。这里讲讲具体实现，一个带参数的XSLT模板：</p>
<pre class="brush:xml;">
&lt;xsl:template name=&quot;FormatDate&quot;&gt;
 &lt;!--Convert Old Date Format:yyyy-mm-dd to New Date Format: dd/mm/yyyy--&gt;
 &lt;xsl:param name=&quot;DateTime&quot; /&gt;
 &lt;xsl:variable name=&quot;y&quot;&gt;
      &lt;xsl:value-of select=&quot;substring($DateTime,1,4)&quot; /&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:variable name=&quot;m_temp&quot;&gt;
  &lt;xsl:value-of select=&quot;substring-after($DateTime,&#39;-&#39;)&quot; /&gt;
 &lt;/xsl:variable&gt;
    &lt;xsl:variable name=&quot;m&quot;&gt;
      &lt;xsl:value-of select=&quot;substring($m_temp,1,2)&quot; /&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:variable name=&quot;d_temp&quot;&gt;
      &lt;xsl:value-of select=&quot;substring-after($m_temp,&#39;-&#39;)&quot; /&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:variable name=&quot;d&quot;&gt;
      &lt;xsl:value-of select=&quot;substring($d_temp,1,2)&quot; /&gt;
    &lt;/xsl:variable&gt;
    &lt;xsl:value-of select=&quot;$d&quot;/&gt;
    &lt;xsl:value-of select=&quot;&#39;/&#39;&quot;/&gt;
    &lt;xsl:value-of select=&quot;$m&quot;/&gt;
    &lt;xsl:value-of select=&quot;&#39;/&#39;&quot;/&gt;
    &lt;xsl:value-of select=&quot;$y&quot;/&gt;
&lt;/xsl:template&gt;</pre>
<p>
 如果对XSLT有所了解，相信上面的代码非常直观；如果不太了解，那么首先你应该去了解，或者在下面留言&hellip;&hellip;</p>
<p>
 在XSLT中，我一直将模板当作其他程序语言中的函数。理解了这一点，上面这段程序就很好理解了，那个参数Datetime就像是程序语言中的入口参数，用来接受外部送给这个&ldquo;函数&rdquo;的参数。</p>
<p>
 需要注意的地方是，上面的19-23行，这是最后对日期各个部分（年、月、日）进行组合。换言之，你可以组合出任意你可以想出的日期格式，不限于此。</p>
<p>
 但这里只写了&ldquo;函数&rdquo;了部分，没有提到调用，调用方法如下：</p>
<pre class="brush:xml;">
&lt;xsl:call-template name=&quot;FormatDate&quot;&gt;
 &lt;xsl:with-param name=&quot;DateTime&quot; select=&quot;date&quot;/&gt;
&lt;/xsl:call-template&gt;</pre>
<p>
 在此例中，我将date这个结点的值传给了&ldquo;函数&rdquo;的参数。</p>
<p>
 完整的XSLT文件和最后的结果请分别<strike>看<a href="/demo/dateformat-in-xslt/format_date.xsl">这里</a>和<a href="/demo/dateformat-in-xslt/test.xml">这里</a></strike>。</p>]]></description>
<author><![CDATA[Saturn]]></author>
<guid isPermaLink="true" >http://www.cnsaturn.com/posts/converting-datetime-format-in-xslt</guid>
</item>
<item>
<title><![CDATA[实例分析setTimeout和setInterval的区别]]></title>
<link>http://www.cnsaturn.com/posts/differences-between-settimeout-and-setinterval-in-jvascript</link>
<pubDate>Wed, 22 Jul 2009 12:30:00 -0400</pubDate>
<description><![CDATA[<p>
 理解编程中某个对象、方法和属性的最好方法，莫过于通过不同实例对其进行验证。</p>
<p>
 在JavaScript中，<a href="http://www.w3schools.com/htmldom/met_win_settimeout.asp">setTimeout</a>和<a href="http://www.w3schools.com/htmldom/met_win_setinterval.asp">setInterval</a>是window对象的两个不同方法；我们通常利用这两个方法来重复执行某个代码段或者function，以实现网页上的一些&ldquo;动态&rdquo;效果。</p>
<p>
 有意思的是，它们的用法几乎完全一样，都是：</p>
<p>
 setTimeOut(<em>expression</em>, <em>timespan</em>)</p>
<p>
 setInterval(<em>expression</em>, <em>timespan</em>)</p>
<p>
 第一个参数expression是需要调用的function；第二个参数timespan是指定延迟（等待）多少秒才调用参数一中的表达式，单位是微秒milliseconds。</p>
<p>
 既然这两个方法都是延迟一段时间后执行某个表达式，那么，它们的功能不就一样了吗？用我们的膝盖想想都应该知道，如果这两个方法的功能完全一样， ECMAScript开发组何不将他们合并成一个方法？</p>
<p>
 </p>]]></description>
<author><![CDATA[Saturn]]></author>
<guid isPermaLink="true" >http://www.cnsaturn.com/posts/differences-between-settimeout-and-setinterval-in-jvascript</guid>
</item>
<item>
<title><![CDATA[Dirty Markup:一个在线精简修复网页文件的工具]]></title>
<link>http://www.cnsaturn.com/posts/a-web-based-html-tiny-interface-called-dirty-markup</link>
<pubDate>Fri, 05 Jun 2009 20:33:00 -0400</pubDate>
<description><![CDATA[<p>
 <a href="http://dirtymarkup.com/">Dirty Markup </a>是一个<a href="http://abeautifulsite.net/notebook/100">abeautifulsite</a>推出的在线修复、整理网页文件的工具。</p>
<p>
 它能够自动修正一个网页文件中潜在的错误HTML或XHTML写法，同时规范化网页源文件布局；使其尽可能符合W3C规范，让一个网页文件最大限度的兼容多浏览器。</p>
<p>
 用一个例子来说明<a href="http://dirtymarkup.com/">Dirty Markup</a>的用法。</p>
<p>
 首先，我写了一个很简单的错误的HTML布局代码。</p>
<pre class="brush:xhtml;">
&lt;div id=h&gt; 
&lt;font color=red&gt;&lt;p&gt;www.cnSaturn.com&lt;/p&gt;
&lt;/font&gt; </pre>
<p>
 以上HTML代码至少有两处错误：</p>
<ol>
 <li>
  <div>
   div标签未闭合</div>
 </li>
 <li>
  块级元素p被font标签所包含</li>
</ol>
<p>
 以上HTML代码还至少有两处安全隐患会导致它在不同浏览器中表现不同：</p>
<ol>
 <li>
  不是一个完整的HTML文件</li>
 <li>
  标签的属性对应的值未使用双引号</li>
</ol>
<p>
 </p>]]></description>
<author><![CDATA[Saturn]]></author>
<guid isPermaLink="true" >http://www.cnsaturn.com/posts/a-web-based-html-tiny-interface-called-dirty-markup</guid>
</item>
<item>
<title><![CDATA[防止AJAX动态结果在前端页面缓存的方法]]></title>
<link>http://www.cnsaturn.com/posts/prevent-ajax-from-caching-data-in-front-page</link>
<pubDate>Sun, 24 May 2009 02:18:00 -0400</pubDate>
<description><![CDATA[<p>
 在利用AJAX从后台程序提取数据，然后在前端页面显示出来的过程中，我们经常会碰到<a target="_blank" title="Ajax不运行解决一例：动态页的客户端网页缓存" xhref="http://www.auiou.com/relevant/00000388.jsp">AJAX提取的数据被缓存</a>，导致结果异常的问题。</p>
<p>
 具体原因是因为AJAX依赖于JavaScript，而浏览器会自动将JavaScript代码和文件数据缓存在客户端，这有利于用户下次访问时页面的快速下载和解析。</p>
<p>
 要解决数据被缓存，大概有两种做法。</p>
<p>
 第一种，是在客户端进行，在AJAX发送请求到后台数据处理页面的URL上加一个<strong>随机字符串</strong> 即可。这样一来，浏览器会认为AJAX发出的HTTP请求每次都不同（URL不同），从而每次都重新请求。具体做法如下：</p>
<pre class="brush:js;">
xmlHttp.open(&quot;GET&quot;, &quot;ajax.php?r=&quot;+new Date().getTime(), true);</pre>
<p>
 既然是随机字符串，还可以使用：+math.random()，只要保持字符串随机即可。</p>
<p>
 另外一种是在服务器端进行。具体做法是修改HTTP头信息，手动设置让其过期。下面以PHP为例说明：</p>
<pre class="brush:php;">
header (&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;); // Date in the past
 header (&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;); // always modified
 header (&quot;Cache-Control: no-cache, must-revalidate&quot;); // HTTP/1.1
 header (&quot;Pragma: no-cache&quot;); // HTTP/1.0</pre>
<p>
 其他语言，比如ASP，J2EE，做法与此非常类似。</p>
<p>
 通常情况下，我们只需要选其以上两种方法的其中一种即可。</p>]]></description>
