学习XSLT(一)

      不太会进行理论的介绍,就拿例子来进行初步的介绍吧!呵呵,望大家见谅!欢迎大家给提意见!
      先看一段XML文档
ContractedBlock.gifExpandedBlockStart.gifXML
None.gif<?xml version="1.0" encoding="utf-8" ?>
None.gif
<Authors>
None.gif  
<Author>
None.gif    
<AuthorName id="1">Author1</AuthorName>
None.gif    
<AuthorAddress>Address1</AuthorAddress>
None.gif    
<AuthorEmail>EMail1</AuthorEmail>
None.gif  
</Author>
None.gif  
<Author>
None.gif    
<AuthorName id="2">Author2</AuthorName>
None.gif    
<AuthorAddress>Address2</AuthorAddress>
None.gif    
<AuthorEmail>EMail2</AuthorEmail>
None.gif  
</Author>
None.gif  
<Author>
None.gif    
<AuthorName>Author3</AuthorName>
None.gif    
<AuthorAddress>Address3</AuthorAddress>
None.gif    
<AuthorEmail>EMail3</AuthorEmail>
None.gif  
</Author>
None.gif
</Authors>
      这段文档经过下面的XSLT文件
None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
None.gif
<xsl:stylesheet version="1.0"
None.gif    xmlns:xsl
="http://www.w3.org/1999/XSL/Transform">
None.gif  
<xsl:output method="xml"/>
None.gif
None.gif  
<xsl:template match="/">
None.gif    
<html>
None.gif      
<body>
None.gif        
<table border="1px">
None.gif        
<xsl:apply-templates/>
None.gif        
</table>
None.gif      
</body>
None.gif    
</html>
None.gif  
</xsl:template>
None.gif  
<xsl:template match="Author">
None.gif    
<xsl:value-of select="."/>
None.gif  
</xsl:template>
None.gif
</xsl:stylesheet>
转换会得到下面的xml文件
ContractedBlock.gifExpandedBlockStart.gifResult
None.gif<?xml version="1.0" encoding="utf-8"?>
None.gif
<html>
None.gif  
<body>
None.gif    
<table border="1px">
None.gif
None.gif      Author1
None.gif      Address1
None.gif      EMail1
None.gif
None.gif
None.gif      Author2
None.gif      Address2
None.gif      EMail2
None.gif
None.gif
None.gif      Author3
None.gif      Address3
None.gif      EMail3
None.gif
None.gif    
</table>
None.gif  
</body>
None.gif
</html>


对于这个.xsl文件:
<xsl:stylesheet>和<xsl:stylesheet>表示这个文档是XSLT样式表,version自然代表使用的版本,xmlns:xsl是标准的XML命名空间声明。所有的XSLT文档必须使用这个URI作为命名空间前缀。当然可以定义自己的命名空间,这样可以避免不同的元素,属性命名的冲突。当然xsl是习惯性的前缀,比较容易理解。也可以使用自定义的前缀名。

<xsl:output method="xml/>用于控制XSLT转换的输出格式(可以指定为xml.html.text),还有其他的可选属性(如version来指明输出xml文档的版本 Encoding指定编码方式)。

<xsl:template match="/"></xsl:template>定义XSLT模板规则,是XSLT中最主要的部分。XSLT代码最终的目的是将XML文档按照模板定义的规则进行输出。属性match用来指定匹配的模式,"/"代表选择根节点,返回的将是根节点后代的集合,具体语法将在下一篇文章中介绍。其它可选属性如name可以定义模板的名字,priority定义模板的优先级,mode定义模板的模式。               name:作为一个模板的标识,就像一个人的名字。如果我们指定一个模板的name="NameXX"那么我们可以通过<xsl:call-template name="NameXX"/>来调用它。本例中<xsl:apply-templates/>就可以用<xsl:call-template name="NameXX"/>代替。               priority指定模板的优先级。                  mode:相当于选择的作用,如果一个模板的mode被设定为"Y",那么<xsl:apply-templates mode="X"/>只会选择与X同名的template,mode值为其它的template将不会得到使用。

<xsl:value-of select="."/>是选择当前节点的值。因为"."匹配当前节点,返回值为当前节点以及其子节点集,所以所有的子节点值会被选择出来。

其他的样式表元素将在第二篇文章中介绍包括xsl:include,xsl:attibute-set,xsl:key,xsl:variable,xsl:param,xsl:if,xsl:when,xsl:otherwise,xsl:choose等!

如果有写的不好的地方希望大家提出来,诚恳的接受批评指导。大家互相交流,共同提高!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值