XSL即可扩展的样式表文件。 可以格式化xml的显示,也可以将xml转换成需要的另一种格式。
学习XSL必须熟悉XPath。XSL和XPath一样简单强大,容易学习。
1. XSL既然可以格式化xml的显示样式,我们先来看如何在xml中引用xsl文件
如下代码示例:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="url.xsl"?>
只需在xml文件的文档声明后面添加<?xml-stylesheet type=”text/xsl” href=”url.xsl”?>即可
2. XSL的格式
XSL也是一个标准的xml文件,它以xml文档声明开始,根元素必须是xsl:styleshee,同时根元素必须有version属性指定 xsl的版本,和xmlns:xsl=” http://www.w3.org/1999/XSL/Transform”指定xsl命名空间,如下示例
<?xml version="1.0" encoding="encoding”?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
3. Xsl要点 如下示例xml
01 |
<?xml version="1.0" encoding="utf-8" ?> |
02 |
<?xml-stylesheet type="text/xsl" href="pets-templates.xsl"?> |
04 |
<pig color="blue" weight="100"> |
06 |
<desc>this is a blue pig</desc> |
08 |
<cat color="red" weight="9"> |
10 |
<desc>this is a red cat</desc> |
12 |
<dog color="green" weight="15"> |
14 |
<desc>this is a green dog</desc> |
16 |
<cat color="green" weight="15"> |
18 |
<desc>this is a green cat</desc> |
22 |
<dog color="blue" weight="10"> |
24 |
<desc>this is a blue dog</desc> |
26 |
<dog color="red" weight="9"> |
28 |
<desc>this is a red dog</desc> |
上面的xml在通过xsl格式化之后的显示效果如下:

1) xsl:template定义匹配节点的转换模板,属性match=”xpath expression”用来定义模板匹配的元素
如下定义匹配根节点的模板
<xsl:template match=”/”>
</xsl:template>
2) xsl:for-each循环显示select=”xpath expression”选择节点的转换 (类似编程语言中的foreach语句),
如下示例,选择了pets下面的子元素,并循环显示子元素的几点名字:
<xsl:for-each select=”/pets/*”>
<xsl:value-of select=”name()”/>
</xsl:for-each>
3) xsl:if 元素条件显示节点(类似编程语言中的if语句)注意小于号大于号要分别用<和>替代
<xsl:if test=”@weight < 10”>
<i>its weight is less than 10 km</i>
</xsl:if>
4) xsl:choose 多分支条件显示 (类似编程语言中的switch语句)
<xsl:choose >
<xsl:when test=”name() = ‘pig’”>
<i>this is a pig</i>
</xsl:when>
<xsl:otherwise>
<i>this is not a pig</i>
</xsl:otherwise>
</xsl:choose>
5) xsl:value-of 显示选择节点或者属性的值
选择子节点price
<xsl:value-of select=”pets/*/price”/>
选择属性weight
<xsl:value-of select=”pets/*/@weight”/>
6) xsl:attribute 构造xml节点的属性
用来向节点添加属性,例如:
<font>
<xsl:attribute name=”color”><xsl:value-of select=”pets/*/@color”/></xsl:attribute>
</font>
将输出<font color=”red”></font>
7) xsl:apply-templates 应用模板
如果xml文件结构比较复杂,可以定义多个template,然后使用<xsl:apply-templates>标签应用模 板,xsl:apply-templates 可以指定属性select=”xpath”来选择应用的模板,或者不指定select表示选择当前节点的模板。
请看下面示例xslt文件pets-templates.xsl
完整的示例xsl文件:pets.xsl
view sourceprint?
01 |
<?xml version="1.0" encoding="utf-8"?> |
02 |
<xsl:stylesheet version="1.0" |
03 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
04 |
<xsl:template match="/"> |
07 |
<META http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
08 |
<title>lovely pets</title> |
09 |
<style type="text/css"> |
10 |
ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;} |
11 |
li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;} |
18 |
<xsl:for-each select="pets/*"> |
22 |
<xsl:when test="name() = 'dog'"> |
23 |
<xsl:attribute name="src">http://estar-tv.com/images/comprofiler/gallery/dog.gif</xsl:attribute> |
25 |
<xsl:when test="name() = 'pig'"> |
26 |
<xsl:attribute name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</xsl:attribute> |
29 |
<xsl:attribute name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</xsl:attribute> |
34 |
<xsl:attribute name="face">Courier</xsl:attribute> |
35 |
<xsl:attribute name="color"> |
36 |
<xsl:value-of select="@color"/> |
38 |
<xsl:value-of select="name()"/> |
39 |
</font> said: "<xsl:value-of select="desc"/>" |
40 |
weight:<xsl:value-of select="@weight"/> |
42 |
<xsl:if test="@weight < 10"> |
44 |
<i>its weight is less than 10 km</i> |
完整示例文件 pets-templates.xsl:
001 |
<?xml version="1.0" encoding="utf-8"?> |
002 |
<xsl:stylesheet version="1.0" |
003 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
004 |
<xsl:template match="/"> |
007 |
<META http-equiv="Content-Type" content="text/html; charset=utf-8"/> |
008 |
<title>lovely pets</title> |
009 |
<style type="text/css"> |
010 |
ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;} |
011 |
li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;} |
018 |
<xsl:apply-templates select="pets" /> |
025 |
<xsl:template match="pets"> |
026 |
<xsl:apply-templates select="dog"></xsl:apply-templates> |
027 |
<xsl:apply-templates select="pig"></xsl:apply-templates> |
028 |
<xsl:apply-templates select="cat"></xsl:apply-templates> |
031 |
<xsl:template match="dog"> |
032 |
<xsl:for-each select="."> |
035 |
<xsl:attribute name="src">http://estar-tv.com/images/comprofiler/gallery/dog.gif</xsl:attribute> |
038 |
<xsl:attribute name="face">Courier</xsl:attribute> |
039 |
<xsl:attribute name="color"> |
040 |
<xsl:value-of select="@color"/> |
043 |
</font> said: "<xsl:value-of select="desc"/>" |
044 |
weight:<xsl:value-of select="@weight"/> |
046 |
<xsl:if test="@weight < 10"> |
048 |
<i>its weight is less than 10 km</i> |
057 |
<xsl:template match="pig"> |
058 |
<xsl:for-each select="."> |
061 |
<xsl:attribute name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</xsl:attribute> |
064 |
<xsl:attribute name="face">Courier</xsl:attribute> |
065 |
<xsl:attribute name="color"> |
066 |
<xsl:value-of select="@color"/> |
069 |
</font> said: "<xsl:value-of select="desc"/>" |
070 |
weight:<xsl:value-of select="@weight"/> |
072 |
<xsl:if test="@weight < 10"> |
074 |
<i>its weight is less than 10 km</i> |
082 |
<xsl:template match="cat"> |
083 |
<xsl:for-each select="."> |
086 |
<xsl:attribute name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</xsl:attribute> |
089 |
<xsl:attribute name="face">Courier</xsl:attribute> |
090 |
<xsl:attribute name="color"> |
091 |
<xsl:value-of select="@color"/> |
094 |
</font> said: "<xsl:value-of select="desc"/>" |
095 |
weight:<xsl:value-of select="@weight"/> |
097 |
<xsl:if test="@weight < 10"> |
099 |
<i>its weight is less than 10 km</i> |
在c#.net中使用XslCompiledTransform转换xml文档,XslTransform也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:
02 |
using System.Collections.Generic; |
12 |
static void Main(string[] args) |
15 |
System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform(); |
17 |
string xsltFile = @"X:\about.net\System.Xml\example\pets.xsl"; |
18 |
using (StreamReader rdr = new StreamReader(xsltFile)) |
20 |
using (XmlReader xmlRdr = XmlReader.Create(rdr)) |
26 |
string inputFile = @"X:\about.net\System.Xml\example\pets.xml"; |
27 |
string outputFile = @"X:\about.net\System.Xml\example\pets-out.htm"; |
28 |
//转化源文件输出到输出文件outputFile |
29 |
trans.Transform(inputFile, outputFile); |
有一点需要注意,使用XslCompiledTransform转换出来的文件,是一个html格式的,这个类会自动在html的head标签中添 加一个未关闭的meta标签 <META http-equiv="Content-Type" content="text/html; charset=utf-8">;微软帮我们想的太多了。
标签:
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@evget.com
文章转载自:网络转载