<转>在xslt 1.0 中取得当前时间
2012-02-14 09:35
155 查看
在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>
注意:下面的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>
相关文章推荐
- 在xslt 1.0 中取得当前时间
- <Java> 获取当前系统时间
- <<时间管理与问题解决技巧>> 培训心得
- python 的常用时间操作,取得当前时间等
- <JAVA>关于日期时间的编写
- PHP之取得当前时间函数方法
- 第九周任务二之定义Time类中的<<和>>运算符重载,实现时间的输入输出
- 如何用PHP中的date()函数取得当前的确切时间
- <java代码> 实现Unix时间戳(Unix timestamp)与普通时间 之间的相互转换
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
- linux 下 C/C++ 实现取得当前时间
- <转>C# 4.0 为泛型编程引入了 协变 和 逆变 支持,这是个不错的福利,能省掉以往的一些麻烦。不过当前(Beta2)仅支持泛型接口和泛型委托。
- <编译>条件编译——判断当前使用的编译器及操作系统
- MySQL取得当前日期固定时间的方法。
- 在XSL中取得当前时间
- <fmt:formatDate> 标签 — 时间格式化
- 在页面格式化获取时间的月份、年份、日(使用<fmt:formatDate> 标签)
- html的<select>标签,如何获取当前选中的项的内容呢?
- SQL 10 函数 3 日期时间函数 - 3 取得当前日期时间
- mssql的日期函数,如何取得当前年月日,当前时间,当前月份,当前天数,一年中第几天?