在xsl 中怎麼顯示目前時間,可以使用微軟的xsl 命名空間定義(一種是URL 命名空間命名法:xmlns:msxsl="http://www.w3.org/TR/WD-xsl" ,一種是URN 命名空間命名法: xmlns:msxsl="urn:schemas-microsoft-com:xslt" ),具體代碼如下,分别建立hello.xsl 檔案和hello.xml 檔案于同一目錄下,用IE 打開hello.xml 即可看到運作結果。
注意:下面的hello.xsl 中實際使用了兩種xsl 命名空間,一種是微軟的 xmlns:msxsl="urn:schemas-microsoft-com:xslt" ,一種是w3 組織的 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 。
hello.xsl :
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:msfunction="http://www.mycompany.org/ns/function" exclude-result-prefixes="msxsl msfunction">
<msxsl:script implements-prefix="msfunction" language="javascript">
<!--[CDATA[
function clock(){
var time = new Date();
var year = time.getFullYear();
var month = time.getMonth() + 1;
var day = time.getDate();
var hours = time.getHours();
var min = time.getMinutes();
var sec = time.getSeconds();
return year + "/" + month + "/" + day + " " + hours + ":" + min + ":" + sec ;
}
]]-->
</msxsl:script>
<xsl:template match="/">
<xsl:value-of select="msfunction:clock()"/>
</xsl:template>
</xsl:stylesheet>
hello.xml :
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl" target="_blank" rel="external nofollow" ?>
<title>Hello, world!</title>
注意 :上面的 xmlns:msxsl="urn:schemas-microsoft-com:xslt" 隻能使用urn 這樣的命名方法,我嘗試使用xmlns:msxsl="http://www.w3.org/TR/WD-xsl" 運作結果會報錯:
使用 XSL 樣式表無法檢視 XML 輸入。請更正錯誤然後單擊 重新整理 按鈕,或以後重試。
名稱空間 'http://www.mycompany.org/ns/function' 不包含任何函數。
另外要注意 msxsl:script 不能在xsl:template 内部使用,否則也會出現上面相同錯誤。
曾嘗試在xsl:template 内部使用
<msxsl:eval language="javascript">clock();</msxsl:eval> 這樣的寫法無法運作出正确結果。