您的位置:首页 > 其它

XSLT文件中内嵌脚本

2005-12-28 17:53 239 查看
最近开始学习XML,对于XML的格式化用XSLT来实现还是不错的,这样在要展示的页面上只须加载XML和XSLT文件的关联即可,要修改样式也不用跑到展示页面了,直接在XSLT里修改即可。不过XSLT用起来不是太顺手,和一般的编程习惯有所不同,但实现却是简单的。以下是我在项目中做的一个XSLT模版,用做读取显示XML的标题链接,并用vbscript来截取指字长度的字符串(中文按2字数计,英文按1字数计。)
<?xml version="1.0" encoding="gb2312" standalone="yes"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="test"
exclude-result-prefixes="msxsl user">
<xsl:output method="html"/>
<msxsl:script language="VBscript" implements-prefix="user">
Function cutstr(txt,length)
txt=trim(txt)
x=len(txt)
y=0
if x>=1 then
for ii=1 to x
acctemp=asc(mid(txt,ii,1))
if (acctemp>255) or (0>acctemp) then '此处因为小于号要做特殊处理,故交换顺序后直接用大于号来得方便
y=y+2
else
y=y+1
end if
if y>=length then
txt=left(trim(txt),ii)
exit for
end if
next
cutstr=txt
else
cutstr=""
end if
End Function
</msxsl:script>

<xsl:template match="/">
<xsl:apply-templates select="rss/channel/item[importance=1]"/>
</xsl:template>
<xsl:template match="rss/channel/item">
<xsl:if test="position()<=6">
<li>
<span class="middate">
[<xsl:value-of select="concat(substring(pubDate,6,2),'/',substring(pubDate,9,2))"/>]
</span>
<a href="{link}" title="{title}" target="_blank">
<xsl:value-of select="user:cutstr(string(title),34)" />
</a>
</li>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

注:XML文件略
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: