<转>在xslt 1.0 中取得当前时间

在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> 这样的写法无法运行出正确结果。

========================my testing ==================================

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="FirstPPEntryDate">
<FirstPPEntryDate type="date" format="dd MMMM yyyy hh:mm:ss aa zz">
<xsl:choose>
<xsl:when test="text()!=''">
<xsl:value-of select="text()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="msfunction:clock()"/>
</xsl:otherwise>
</xsl:choose>
</FirstPPEntryDate>
< /xsl:template>
< /xsl:stylesheet>

hello.xml:

<?xml version="1.0" encoding="iso-8859-1"?>

< ?xml-stylesheet type="text/xsl" href="hello.xsl"?>


< FirstPPEntryDate type="Date" format="dd MMMM yyyy hh:mm:ss aa zz">22 September 2010 12:06:12 AM SGT</FirstPPEntryDate>

原文地址:https://www.cnblogs.com/sunny0515/p/2350408.html