您的位置:首页 > 其它

XSLT语法 在.net中使用XSLT转换xml文档示例

2011-07-26 22:46 633 查看
XSL即可扩展的样式表文件。可以格式化xml的显示,也可以将xml转换成需要的另一种格式。

学习XSL必须熟悉XPath。XSL和XPath一样简单强大,容易学习。

1.XSL既然可以格式化xml的显示样式,我们先来看如何在xml中引用xsl文件

如下代码示例:

<?xmlversion="1.0"encoding="utf-8"?>

<?xml-stylesheettype="text/xsl"href="url.xsl"?>

只需在xml文件的文档声明后面添加<?xml-stylesheettype=”text/xsl”href=”url.xsl”?>即可

2.XSL的格式

XSL也是一个标准的xml文件,它以xml文档声明开始,根元素必须是xsl:styleshee,同时根元素必须有version属性指定xsl的版本,和xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”指定xsl命名空间,如下示例
<?xmlversion="1.0"encoding="encoding”?>

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

3.Xsl要点如下示例xml

viewsourceprint?

01
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
02
<?
xml-stylesheet
type
=
"text/xsl"
href
=
"pets-templates.xsl"
?>
03
<
pets
>
04
<
pig
color
=
"blue"
weight
=
"100"
>
05
<
price
>100</
price
>
06
<
desc
>thisisabluepig</
desc
>
07
</
pig
>
08
<
cat
color
=
"red"
weight
=
"9"
>
09
<
price
>80</
price
>
10
<
desc
>thisisaredcat</
desc
>
11
</
cat
>
12
<
dog
color
=
"green"
weight
=
"15"
>
13
<
price
>80</
price
>
14
<
desc
>thisisagreendog</
desc
>
15
</
dog
>
16
<
cat
color
=
"green"
weight
=
"15"
>
17
<
price
>80</
price
>
18
<
desc
>thisisagreencat</
desc
>
19
</
cat
>
20
21
22
<
dog
color
=
"blue"
weight
=
"10"
>
23
<
price
>100</
price
>
24
<
desc
>thisisabluedog</
desc
>
25
</
dog
>
26
<
dog
color
=
"red"
weight
=
"9"
>
27
<
price
>80</
price
>
28
<
desc
>thisisareddog</
desc
>
29
</
dog
>
30
</
pets
>
上面的xml在通过xsl格式化之后的显示效果如下:



1)xsl:template定义匹配节点的转换模板,属性match=”xpathexpression”用来定义模板匹配的元素

如下定义匹配根节点的模板

<xsl:templatematch=”/”>

</xsl:template>

2)xsl:for-each循环显示select=”xpathexpression”选择节点的转换(类似编程语言中的foreach语句),

如下示例,选择了pets下面的子元素,并循环显示子元素的几点名字:

<xsl:for-eachselect=”/pets/*”>

<xsl:value-ofselect=”name()”/>

</xsl:for-each>

3)xsl:if元素条件显示节点(类似编程语言中的if语句)注意小于号大于号要分别用<和>替代

<xsl:iftest=”@weight<10”>

<i>itsweightislessthan10km</i>

</xsl:if>

4)xsl:choose多分支条件显示(类似编程语言中的switch语句)

<xsl:choose>

<xsl:whentest=”name()=‘pig’”>

<i>thisisapig</i>

</xsl:when>

<xsl:otherwise>

<i>thisisnotapig</i>

</xsl:otherwise>

</xsl:choose>

5)xsl:value-of显示选择节点或者属性的值

选择子节点price

<xsl:value-ofselect=”pets/*/price”/>

选择属性weight

<xsl:value-ofselect=”pets/*/@weight”/>

6)xsl:attribute构造xml节点的属性

用来向节点添加属性,例如:

<font>

<xsl:attributename=”color”><xsl:value-ofselect=”pets/*/@color”/></xsl:attribute>

</font>

将输出<fontcolor=”red”></font>

7)xsl:apply-templates应用模板

如果xml文件结构比较复杂,可以定义多个template,然后使用<xsl:apply-templates>标签应用模板,xsl:apply-templates可以指定属性select=”xpath”来选择应用的模板,或者不指定select表示选择当前节点的模板。

请看下面示例xslt文件pets-templates.xsl

完整的示例xsl文件:pets.xsl

viewsourceprint?

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
=
"/"
>
05
<
html
>
06
<
head
>
07
<
META
http-equiv
=
"Content-Type"
content
=
"text/html;charset=utf-8"
/>
08
<
title
>lovelypets</
title
>
09
<
style
type
=
"text/css"
>
10
ul{margin:10px010px0;padding:0;width:400px;text-align:left;}
11
li{height:60px;display:block;list-style:none;padding:4px;border:1pxsolid#f0f0f0;margin:5px;}
12
</
style
>
13
</
head
>
14
<
body
>
15
<
center
>
16
<
h1
>lovelypets</
h1
>
17
<
ul
>
18
<
xsl:for-each
select
=
"pets/*"
>
19
<
li
>
20
<
img
align
=
"right"
>
21
<
xsl:choose
>
22
<
xsl:when
test
=
"name()='dog'"
>
23
<
xsl:attribute
name
=
"src"
>http://estar-tv.com/images/comprofiler/gallery/dog.gif</
xsl:attribute
>
24
</
xsl:when
>
25
<
xsl:when
test
=
"name()='pig'"
>
26
<
xsl:attribute
name
=
"src"
>http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</
xsl:attribute
>
27
</
xsl:when
>
28
<
xsl:otherwise
>
29
<
xsl:attribute
name
=
"src"
>http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</
xsl:attribute
>
30
</
xsl:otherwise
>
31
</
xsl:choose
>
32
</
img
>
33
<
font
>
34
<
xsl:attribute
name
=
"face"
>Courier</
xsl:attribute
>
35
<
xsl:attribute
name
=
"color"
>
36
<
xsl:value-of
select
=
"@color"
/>
37
</
xsl:attribute
>
38
<
xsl:value-of
select
=
"name()"
/>
39
</
font
>said:"<
xsl:value-of
select
=
"desc"
/>"
40
weight:<
xsl:value-of
select
=
"@weight"
/>
41
42
<
xsl:if
test
=
"@weight<10"
>
43
<
p
>
44
<
i
>itsweightislessthan10km</
i
>
45
</
p
>
46
</
xsl:if
>
47
48
49
</
li
>
50
</
xsl:for-each
>
51
</
ul
>
52
</
center
>
53
</
body
>
54
</
html
>
55
</
xsl:template
>
56
</
xsl:stylesheet
>
完整示例文件pets-templates.xsl:

viewsourceprint?

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
=
"/"
>
005
<
html
>
006
<
head
>
007
<
META
http-equiv
=
"Content-Type"
content
=
"text/html;charset=utf-8"
/>
008
<
title
>lovelypets</
title
>
009
<
style
type
=
"text/css"
>
010
ul{margin:10px010px0;padding:0;width:400px;text-align:left;}
011
li{height:60px;display:block;list-style:none;padding:4px;border:1pxsolid#f0f0f0;margin:5px;}
012
</
style
>
013
</
head
>
014
<
body
>
015
<
center
>
016
<
h1
>lovelypets</
h1
>
017
<
ul
>
018
<
xsl:apply-templates
select
=
"pets"
/>
019
</
ul
>
020
</
center
>
021
</
body
>
022
</
html
>
023
</
xsl:template
>
024
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
>
029
</
xsl:template
>
030
031
<
xsl:template
match
=
"dog"
>
032
<
xsl:for-each
select
=
"."
>
033
<
li
>
034
<
img
align
=
"right"
>
035
<
xsl:attribute
name
=
"src"
>http://estar-tv.com/images/comprofiler/gallery/dog.gif</
xsl:attribute
>
036
</
img
>
037
<
font
>
038
<
xsl:attribute
name
=
"face"
>Courier</
xsl:attribute
>
039
<
xsl:attribute
name
=
"color"
>
040
<
xsl:value-of
select
=
"@color"
/>
041
</
xsl:attribute
>
042
dog
043
</
font
>said:"<
xsl:value-of
select
=
"desc"
/>"
044
weight:<
xsl:value-of
select
=
"@weight"
/>
045
046
<
xsl:if
test
=
"@weight<10"
>
047
<
p
>
048
<
i
>itsweightislessthan10km</
i
>
049
</
p
>
050
</
xsl:if
>
051
</
li
>
052
</
xsl:for-each
>
053
</
xsl:template
>
054
055
056
057
<
xsl:template
match
=
"pig"
>
058
<
xsl:for-each
select
=
"."
>
059
<
li
>
060
<
img
align
=
"right"
>
061
<
xsl:attribute
name
=
"src"
>http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</
xsl:attribute
>
062
</
img
>
063
<
font
>
064
<
xsl:attribute
name
=
"face"
>Courier</
xsl:attribute
>
065
<
xsl:attribute
name
=
"color"
>
066
<
xsl:value-of
select
=
"@color"
/>
067
</
xsl:attribute
>
068
pig
069
</
font
>said:"<
xsl:value-of
select
=
"desc"
/>"
070
weight:<
xsl:value-of
select
=
"@weight"
/>
071
072
<
xsl:if
test
=
"@weight<10"
>
073
<
p
>
074
<
i
>itsweightislessthan10km</
i
>
075
</
p
>
076
</
xsl:if
>
077
</
li
>
078
</
xsl:for-each
>
079
</
xsl:template
>
080
081
082
<
xsl:template
match
=
"cat"
>
083
<
xsl:for-each
select
=
"."
>
084
<
li
>
085
<
img
align
=
"right"
>
086
<
xsl:attribute
name
=
"src"
>http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</
xsl:attribute
>
087
</
img
>
088
<
font
>
089
<
xsl:attribute
name
=
"face"
>Courier</
xsl:attribute
>
090
<
xsl:attribute
name
=
"color"
>
091
<
xsl:value-of
select
=
"@color"
/>
092
</
xsl:attribute
>
093
cat
094
</
font
>said:"<
xsl:value-of
select
=
"desc"
/>"
095
weight:<
xsl:value-of
select
=
"@weight"
/>
096
097
<
xsl:if
test
=
"@weight<10"
>
098
<
p
>
099
<
i
>itsweightislessthan10km</
i
>
100
</
p
>
101
</
xsl:if
>
102
</
li
>
103
</
xsl:for-each
>
104
</
xsl:template
>
105
106
</
xsl:stylesheet
>
在c#.net中使用XslCompiledTransform转换xml文档,XslTransform也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:

viewsourceprint?

01
using
System;
02
using
System.Collections.Generic;
03
using
System.Linq;
04
using
System.Text;
05
using
System.IO;
06
using
System.Xml;
07
08
namespace
UseXslt
09
{
10
class
Program
11
{
12
static
void
Main(
string
[]args)
13
{
14
//声明XslTransform类实例
15
System.Xml.Xsl.XslCompiledTransformtrans=
new
System.Xml.Xsl.XslCompiledTransform();
16
17
string
xsltFile=
@"X:\about.net\System.Xml\example\pets.xsl"
;
18
using
(StreamReaderrdr=
new
StreamReader(xsltFile))
19
{
20
using
(XmlReaderxmlRdr=XmlReader.Create(rdr))
21
{
22
//载入xsl文件
23
trans.Load(xmlRdr);
24
}
25
}
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);
30
}
31
}
32
}
有一点需要注意,使用XslCompiledTransform转换出来的文件,是一个html格式的,这个类会自动在html的head标签中添加一个未关闭的meta标签<METAhttp-equiv="Content-Type"content="text/html;charset=utf-8">;微软帮我们想的太多了。

Xslt还可以指定参数,定义变量,有关这些方面请查看相关文档。

C#处理Xml的相关随笔:

1.通过XmlDocument读写Xml文档
2.使用XmlReader读Xml,使用XmlWriter写Xml
3.使用Linqtoxml存取XML
4.通过XmlScheme定义固定格式xml文档
5.Xml序列化或者反序列化类
6.通过XPath查找Xml节点
7.通过Xslt转化Xml格式
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: