显示标签为“XSLT”的博文。显示所有博文
显示标签为“XSLT”的博文。显示所有博文

2008年10月25日星期六

embed and run javascript in xslt in MSIE;在MSIE平台XML/XSLT中嵌入并运行javascript

we can embed javascript in XSLT and run it after transform on Mircrosoft IE platform.

the trick is put javascript code in a seperate procedure template, and let transform engine treat content as html code.

then, we must escape the javascript code, this means we use "&lt;" to replace "<" and "&gt;" to replace ">", otherwise the xslt file will be illformed.

afte this, we use <xsl:call-template name=""/> to call the javascript template.



<!--in main template-->
....
<xsl:call-template name="jsproc"/>
....

<!--javascript template-->
<xsl:template name="jsproc">
<xsl:text disable-output-escaping="yes">
&lt;script type="text/javascript"&gt;
......
&lt;/script&gt;
</xsl:text>
</xsl:template>
>>查看全文...

2008年9月13日星期六

XML/XSLT客户端转换

当前主流浏览器都开始支持XML/XSLT转换,建立以XML/XSLT技术为基础的网站成为可能。具体方法是,服务器端返回一个XML文件,这个XML文件引用某个(比如服务器上的)XSLT文件,浏览器以XSLT为模板将XML文件转换为XHTML显示出来,这样服务器可以提供XML而不是HTML给用户来显示网络页面。

这种技术的优点有:
1,呈现工作由客户端完成,减少服务器压力。
2,XML文件小于XHTML文件,减少带宽。
3,有可能通过不同的XSLT文件提供不同的最终显示。
4,XSLT支持有限的编程语言功能,可以在客户端对显示进行选择和调整而不必消费服务的CPU。
5,XSLT不依赖于脚本,Cookie等功能,不受浏览器安全等级影响。

缺点有:
1,运行javascript困难。(不是不能运行)。
2,IE6.0以上对XML/XSLT支持完整。FireFox的支持不完整。
3,非常严重的问题,搜索引擎可以收录XML,但不使用XSLT,导致诸多问题。

由于以上问题,当前此项技术实际应用还比较少。知名网站中CSDN采用了此技术。我的小网站野百合资讯网在线工具网采用了这个技术.
一般多采取在服务器端将XML转化为XHTML,再返回给用户。
>>查看全文...

XSLT里的函数循环,loop in xslt

XSLT没有提循环功能,但可以通xsl:call-template递归调用来模拟循环功能。示例:

<xsl:template name="loopfunc">

<xsl:param name="start"/>

<xsl:param name="end"/>



.......



<xsl:if test="$start &lt; $end">

<xsl:call-template name="loopfunc">

<xsl:with-param name="start"><xsl:value-of select="$start+1"/></xsl:with-param>

<xsl:with-param name="end" ><xsl:value-of select="$end"/></xsl:with-param>

</xsl:call-template>

</xsl:if>

</xsl:template>



可以看到,在这个模板结束的地方,再次调用自身,并将一个参数增加1,用于判断循环结束。
>>查看全文...

xsl:call-template,XSLT里的函数调用

Through xsl:call-template, XSLT can mimic call another template like normal programming language. and the caller can provide named parameter(s) through xsl:with-param, and the callee can declare param througn xsl:param element. the callee access these parameter(s) through a $ symbol plus the parameter's name. 



通过xsl:call-template功能,XSLT可以实现一定的函数调用功能。如下例所示,一个模板可以调用另一个模板,并提供相应的参数。被调用的模板可以使用$paramname访问参数,同时也可以访问当前处理的XML文件。

<!--the main template主叫模板-->

<xsl:template name="maintemplate"> 

...........

<xsl:call-template name="linklist">

<xsl:with-param name="start" select="1"/>

<xsl:with-param name="end" select="//totalpage"/>

</xsl:call-template> 

........

</xsl:template>







<!--a template been called被调用的模板-->

<xsl:template name="subtemplate">

<xsl:param name="start"/>

<xsl:param name="end"/>

<xsl:choose>

<xsl:when test="$start=1">

........

</xsl:when>

<xsl:otherwise> 

......

</xsl:otherwise>

</xsl:choose>

</xsl:if>

</xsl:template>


>>查看全文...

xsl:import and xsl:include的区别和应用

xsl:inculde的功能类似C语言中的include,只是一种简单地引用,转换工作仍然按原来的顺序进行。

而xsl:import则试图模仿类继承,转换将从被import的XSLT开始进行,如果多重import,则从最高处的XSLT开始。

比如可以设一个基本的XSLT文件为不同的XML/XSLT对提供基本的框架,然后在不同的XSLT里提供内容细节,有点类似模版页面,或者ASP.NET里的masterpage功能。



比如,基本框架可以是一个被各个页面import的XSLT



<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<!--主框架-->

<xsl:template match="/">

<html>

<head><title></title>

</head><body> 

.....

<xsl:apply-templates />

.....

</body></html></xsl:template>



注意这里的<xsl:apply-templates />,它指示处理器去寻找合适的template,比如与不同页面相对应的XSLT template,这样就可以输出相应的内容。
>>查看全文...