版權所有:(xiaodaoxiaodao)藍小刀
??
xiaodaoxiaodao@gmail.com
http://www.tkk7.com/xiaodaoxiaodao/archive/2007/09/03/142428.html
?
??
??
??
轉載請注明來源/作者
?
在XSL中取得當前時間
?
在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"?>
<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>
這樣的寫法無法運行出正確結果。
?
?
版權所有:(xiaodaoxiaodao)藍小刀
??
xiaodaoxiaodao@gmail.com
?
?