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

前端之家收集整理的这篇文章主要介绍了XSLT语法 在.net中使用XSLT转换xml文档示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

1 @H_301_39@
2 @H_301_39@
3 @H_301_39@
4 @H_301_39@
5 @H_301_39@
6 @H_301_39@
7 @H_301_39@
8 @H_301_39@
9 @H_301_39@
10 @H_301_39@
11 @H_301_39@
12 @H_301_39@
13 @H_301_39@
14 @H_301_39@
15 @H_301_39@
16 @H_301_39@
17 @H_301_39@
18 @H_301_39@
19 @H_301_39@
20 @H_301_39@
21 @H_301_39@
22 @H_301_39@
23 @H_301_39@
24 @H_301_39@
25 @H_301_39@
26 @H_301_39@
27 @H_301_39@
28 @H_301_39@
29 @H_301_39@
30 @H_301_39@
<? xml version="1.0" encoding="utf-8" ?> @H_301_39@
xml-stylesheet type="text/xsl" href="pets-templates.xsl"?> @H_301_39@
< pets > @H_301_39@
pig color="blue" weight="100"> @H_301_39@
price >100</ > @H_301_39@
desc >this is a blue pig</ > @H_301_39@
</ > @H_301_39@
cat color="red" weight="9"> @H_301_39@
>80</ > @H_301_39@
>this is a red cat</ > @H_301_39@
> @H_301_39@
dog color="green" weight="15"> @H_301_39@
> @H_301_39@
>this is a green dog</ > @H_301_39@
> @H_301_39@
color="green" weight="15"> @H_301_39@
> @H_301_39@
>this is a green cat</ > @H_301_39@
> @H_301_39@
@H_301_39@
@H_301_39@
color="blue" weight="10"> @H_301_39@
> @H_301_39@
>this is a blue dog</ > @H_301_39@
> @H_301_39@
color="red" weight="9"> @H_301_39@
> @H_301_39@
>this is a red dog</ > @H_301_39@
> @H_301_39@
> @H_301_39@ @H_301_39@
@H_301_39@ @H_301_39@ @H_301_39@

上面的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语句)注意小于号大于号要分别用&lt;和&gt;替代

<xsl:if test=”@weight &lt; 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 @H_301_39@
1 @H_301_39@
2 @H_301_39@
3 @H_301_39@
4 @H_301_39@
5 @H_301_39@
6 @H_301_39@
7 @H_301_39@
8 @H_301_39@
9 @H_301_39@
10 @H_301_39@
11 @H_301_39@
12 @H_301_39@
13 @H_301_39@
14 @H_301_39@
15 @H_301_39@
16 @H_301_39@
17 @H_301_39@
18 @H_301_39@
19 @H_301_39@
20 @H_301_39@
21 @H_301_39@
22 @H_301_39@
23 @H_301_39@
24 @H_301_39@
25 @H_301_39@
26 @H_301_39@
27 @H_301_39@
28 @H_301_39@
29 @H_301_39@
30 @H_301_39@
31 @H_301_39@
32 @H_301_39@
33 @H_301_39@
34 @H_301_39@
35 @H_301_39@
36 @H_301_39@
37 @H_301_39@
38 @H_301_39@
39 @H_301_39@
40 @H_301_39@
41 @H_301_39@
42 @H_301_39@
43 @H_301_39@
44 @H_301_39@
45 @H_301_39@
46 @H_301_39@
47 @H_301_39@
48 @H_301_39@
49 @H_301_39@
50 @H_301_39@
51 @H_301_39@
52 @H_301_39@
53 @H_301_39@
54 @H_301_39@
55 @H_301_39@
56 @H_301_39@
<? xml version="1.0" encoding="utf-8"?> @H_301_39@
< xsl:stylesheet version="1.0" @H_301_39@
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> @H_301_39@
xsl:template match="/"> @H_301_39@
html > @H_301_39@
head > @H_301_39@
Meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> @H_301_39@
title >lovely pets</ > @H_301_39@
style type="text/css"> @H_301_39@
ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;} @H_301_39@
li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;} @H_301_39@
</ > @H_301_39@
> @H_301_39@
body > @H_301_39@
center > @H_301_39@
h1 > @H_301_39@
ul > @H_301_39@
xsl:for-each select="pets/*"> @H_301_39@
li > @H_301_39@
img align="right"> @H_301_39@
xsl:choose > @H_301_39@
xsl:when test="name() = 'dog'"> @H_301_39@
xsl:attribute name="src">http://estar-tv.com/images/comprofiler/gallery/dog.gif</ > @H_301_39@
> @H_301_39@
test="name() = 'pig'"> @H_301_39@
name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</ > @H_301_39@
> @H_301_39@
xsl:otherwise > @H_301_39@
name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418</ > @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
font > @H_301_39@
name="face">Courier</ > @H_301_39@
name="color"> @H_301_39@
xsl:value-of select="@color"/> @H_301_39@
> @H_301_39@
select="name()"/> @H_301_39@
> said: "< select="desc"/>" @H_301_39@
weight:< select="@weight"/> @H_301_39@
@H_301_39@
xsl:if test="@weight < 10"> @H_301_39@
p > @H_301_39@
i >its weight is less than 10 km</ > @H_301_39@
> @H_301_39@
> @H_301_39@
@H_301_39@
@H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@ @H_301_39@
@H_301_39@ @H_301_39@ @H_301_39@

完整示例文件 pets-templates.xsl:

56 @H_301_39@
57 @H_301_39@
58 @H_301_39@
59 @H_301_39@
60 @H_301_39@
61 @H_301_39@
62 @H_301_39@
63 @H_301_39@
64 @H_301_39@
65 @H_301_39@
66 @H_301_39@
67 @H_301_39@
68 @H_301_39@
69 @H_301_39@
70 @H_301_39@
71 @H_301_39@
72 @H_301_39@
73 @H_301_39@
74 @H_301_39@
75 @H_301_39@
76 @H_301_39@
77 @H_301_39@
78 @H_301_39@
79 @H_301_39@
80 @H_301_39@
81 @H_301_39@
82 @H_301_39@
83 @H_301_39@
84 @H_301_39@
85 @H_301_39@
86 @H_301_39@
87 @H_301_39@
88 @H_301_39@
89 @H_301_39@
90 @H_301_39@
91 @H_301_39@
92 @H_301_39@
93 @H_301_39@
94 @H_301_39@
95 @H_301_39@
96 @H_301_39@
97 @H_301_39@
98 @H_301_39@
99 @H_301_39@
100 @H_301_39@
101 @H_301_39@
102 @H_301_39@
103 @H_301_39@
104 @H_301_39@
105 @H_301_39@
106 @H_301_39@ xsl:apply-templates select="pets" /> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
@H_301_39@
match="pets"> @H_301_39@
select="dog"></ select="pig"></ select="cat"></ > @H_301_39@
@H_301_39@
match="dog"> @H_301_39@
select="."> @H_301_39@
align="right"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
name="color"> @H_301_39@
select="@color"/> @H_301_39@
> @H_301_39@
dog @H_301_39@
select="desc"/>" @H_301_39@
select="@weight"/> @H_301_39@
@H_301_39@
test="@weight < 10"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
@H_301_39@
@H_301_39@
@H_301_39@
match="pig"> @H_301_39@
select="."> @H_301_39@
> @H_301_39@
align="right"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
name="color"> @H_301_39@
select="@color"/> @H_301_39@
> @H_301_39@
pig @H_301_39@
select="desc"/>" @H_301_39@
select="@weight"/> @H_301_39@
@H_301_39@
test="@weight < 10"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
@H_301_39@
@H_301_39@
match="cat"> @H_301_39@
select="."> @H_301_39@
> @H_301_39@
align="right"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
name="color"> @H_301_39@
select="@color"/> @H_301_39@
> @H_301_39@
cat @H_301_39@
select="desc"/>" @H_301_39@
select="@weight"/> @H_301_39@
@H_301_39@
test="@weight < 10"> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
> @H_301_39@
@H_301_39@
> @H_301_39@ @H_301_39@ @H_301_39@ @H_301_39@ @H_301_39@

在c#.net中使用XslCompiledTransform转换xml文档,XslTransform也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:

32 @H_301_39@ using System; @H_301_39@
System.Collections.Generic; @H_301_39@
System.Linq; @H_301_39@
System.Text; @H_301_39@
System.IO; @H_301_39@
System.Xml; @H_301_39@ @H_301_1151@ @H_301_39@
namespace UseXslt @H_301_39@
{ @H_301_39@
class Program @H_301_39@
{ @H_301_39@
static void Main( string [] args) @H_301_39@
{ @H_301_39@
//声明XslTransform类实例 @H_301_39@
System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform(); @H_301_39@
@H_301_39@
xsltFile = @"X:\about.net\System.Xml\example\pets.xsl" ; @H_301_39@
(StreamReader rdr = StreamReader(xsltFile)) @H_301_39@
{ @H_301_39@
(XmlReader xmlRdr = XmlReader.Create(rdr)) @H_301_39@
{ @H_301_39@
//载入xsl文件 @H_301_39@
trans.Load(xmlRdr); @H_301_39@
} @H_301_39@
} @H_301_39@
inputFile = @"X:\about.net\System.Xml\example\pets.xml" ; @H_301_39@
outputFile = @"X:\about.net\System.Xml\example\pets-out.htm" ; @H_301_39@
//转化源文件输出输出文件outputFile @H_301_39@
trans.Transform(inputFile,outputFile); @H_301_39@
} @H_301_39@
} @H_301_39@
} @H_301_39@ @H_301_39@ @H_301_39@ @H_301_39@ @H_301_39@

有一点需要注意,使用XslCompiledTransform转换出来的文件,是一个html格式的,这个类会自动在html的head标签添加一个未关闭Meta标签 <Meta http-equiv="Content-Type" content="text/html; charset=utf-8">;微软帮我们想的太多了。

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

@H_301_39@ @H_301_39@ 原文链接:https://www.f2er.com/xml/294924.html

猜你在找的XML相关文章