xslt之初步 [转]

首先感性的让我们认识一下xslt。
XSLT是一种用来转换XML文档结构的语言。就是把xml中的节点等信息(你可以把xml看成是数据的存储库),转换为其他的
格式(html等)(结合XSL-FO可以转换为PDF、PS、DOC) ,把xml中的数据信息以html的方式给你呈现出来。目前XSLT最主要的功能还是将XML转换为HTML。

就把我今天研究的初步成果show一下,对于老手或其他的什么手来说太简单拉,但是对于一个以前从没搞过或者从没听说过xslt的朋友还是有点帮助的!让我们共同学习!

先来看个demo:
Hello.xml


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
<document>
<overred>overred</overred>
<said>This said </said>
<write>this is write </write>
</document>

Hello.xsl


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

<xsl:template match="said">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>

<xsl:template match="write">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>
</xsl:stylesheet>



我先对这两个文件的格式做个简单的说明:

xml

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="hello.xsl"?>
就是指定你引用的哪个xsl的地址

<document>
<said>This said </said>
<write>this is write </write>
</document>
是自己定义的节和元素。这个是简单的定义,你也可以把他定义为:

<document>
<content>
<said>This said1 </said>
<write>this is write1 </write>
</content>
<content>
<said>
<aa>This aa</aa>
</said>
<write>this is write2</write>
</content>
</document>

xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
这个是头元素

<xsl:template match="said">
<p>
<xsl:value-of select="."/>
</p>
</xsl:template>

是模板,就是要显示的节点下的元素,match="said"指定是哪个节点下, 
<xsl:value-of select="."/>表示显示这个节点下的所有元素,
<xsl:value-of select="aa"/>,则说明显示这个节点下的aa元素。

对就这两个文件,当你打开Hello.xml的时候就可以看到以下的效果:
This said1

this is write1
是以html的方式呈现的,但是你看代码确实:

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

<said>This said1 </said>
<write>this is write1 </write>


</document>
和Hello.xml一样吧,但是没Hello.xsl他是显示:
<?xml version="1.0" ?>
<document>
<content>
<said>This said1</said>
<write>this is write1</write>
</content>
<content>
<said>
<aa>This aa</aa>
</said>
<write>this is write2</write>
</content>
</document>

现在你明白这个Hello.xsl的作用拉吧。
It So easy!Let us Go
待续~~~~~ 
posted @ 2006-02-15 13:49  blueKnight  Views(180)  Comments(0Edit  收藏  举报