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文件略
<?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文件略
相关文章推荐
- rails的erb模板文件内嵌js脚本的压缩
- XSLT (3)内嵌脚本
- 【程序员技术练级】学习一门脚本语言 python(二)遍历本地文件系统
- linux shell 脚本读取 ini 配置文件
- 通过Windows批处理脚本实现FTP的多文件自动上传
- bat脚本复制拷贝文件例子(当前目录或者是上一层目录)
- Linux中使用shell脚本向文件中写数据
- Python删除Delphi临时文件的小脚本
- Linux shell脚本以单文件解压的形式解压多个文件
- Linux 脚本命令结果输出到文件
- “脚本编辑器”远程文件编辑漏洞
- 树莓派Shell脚本文件
- Linux| 脚本运行错误(坏的解释器:没有那个文件或目录)
- Js脚本文件编译进dll
- LR-FTP下载保存文件脚本
- linux shell脚本 springboot项目发布的shell脚本 包括日志输出到指定的文件
- Linux系统命令及Shell脚本学习笔记三:文件管理
- Python开发.py脚本文件中的#!/usr/bin/python是什么意思?
- shell文件权限和脚本执行
- 脚本检测文件大小