XSLT语法—在.net中使用XSLT转换xml文档的示例代码详解

黄舟
发布: 2017-03-09 16:59:10
原创
1562人浏览过

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.php.cn/”指定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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<?xml version="1.0" encoding="utf-8" ?>

<?xml-stylesheet  type="text/xsl" href="pets-templates.xsl"?>

<pets>

  <pig color="blue" weight="100">

    <price>100</price>

    <desc>this is a blue pig</desc>

  </pig>

  <cat color="red" weight="9">

    <price>80</price>

    <desc>this is a red cat</desc>

  </cat>

  <dog color="green" weight="15">

    <price>80</price>

    <desc>this is a green dog</desc>

  </dog>

  <cat color="green" weight="15">

    <price>80</price>

    <desc>this is a green cat</desc>

  </cat>

 

 

  <dog color="blue" weight="10">

    <price>100</price>

    <desc>this is a blue dog</desc>

  </dog>

  <dog color="red" weight="9">

    <price>80</price>

    <desc>this is a red dog</desc>

  </dog>

</pets>

登录后复制

上面的xml在通过xsl格式化之后的显示效果如下:

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

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

1

2

<xsl:template match=”/”>

</xsl:template>

登录后复制

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

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

1

2

3

<xsl:for-each select=”/pets/*”>

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

</xsl:for-each>

登录后复制

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

法语写作助手
法语写作助手

法语助手旗下的AI智能写作平台,支持语法、拼写自动纠错,一键改写、润色你的法语作文。

法语写作助手31
查看详情 法语写作助手

1

2

3

<xsl:if test=”@weight < 10”>

<i>its weight is less than 10 km</i>

</xsl:if>

登录后复制

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

1

2

3

4

5

6

7

8

<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

1

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

登录后复制

选择属性weight

1

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

登录后复制

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

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

1

2

3

<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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

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

  <xsl:template match="/">

    <html>

      <head>

        <META http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <title>lovely pets</title>

        <style type="text/css">

          ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;}

          li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}

        </style>

      </head>

      <body>

        <center>

        <h1>lovely pets</h1>

        <ul>

          <xsl:for-each select="pets/*">

            <li>

              <img align="right">

                <xsl:choose>

                  <xsl:when test="name() = 'dog'">

                    <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute>

                  </xsl:when>

                  <xsl:when test="name() = 'pig'">

                    <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute>

                  </xsl:when>

                  <xsl:otherwise>

                    <xsl:attribute name="src">http://www.php.cn/@N00.jpg?1143660418</xsl:attribute>

                  </xsl:otherwise>

                </xsl:choose>

              </img>

              <font>

                <xsl:attribute name="face">Courier</xsl:attribute>

                <xsl:attribute name="color">

                  <xsl:value-of select="@color"/>

                </xsl:attribute>

                <xsl:value-of select="name()"/>

              </font> said: "<xsl:value-of select="desc"/>"

              weight:<xsl:value-of select="@weight"/>

 

              <xsl:if test="@weight < 10">

                <p>

                  <i>its weight is less than 10 km</i>

                </p>

              </xsl:if>

 

 

            </li>

          </xsl:for-each>

        </ul>

        </center>

      </body>

    </html>

  </xsl:template>

</xsl:stylesheet>

登录后复制

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"

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

  <xsl:template match="/">

    <html>

      <head>

        <META http-equiv="Content-Type" content="text/html; charset=utf-8"/>

        <title>lovely pets</title>

        <style type="text/css">

          ul{margin:10px 0 10px 0;padding:0;width:400px;text-align:left;}

          li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}

        </style>

      </head>

      <body>

        <center>

          <h1>lovely pets</h1>

          <ul>

            <xsl:apply-templates select="pets" />

          </ul>

        </center>

      </body>

    </html>

  </xsl:template>

 

  <xsl:template match="pets">   

    <xsl:apply-templates select="dog"></xsl:apply-templates>

    <xsl:apply-templates select="pig"></xsl:apply-templates>

    <xsl:apply-templates select="cat"></xsl:apply-templates>

  </xsl:template>

 

  <xsl:template match="dog">

    <xsl:for-each select=".">

      <li>

        <img align="right">

          <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute>         

        </img>

        <font>

          <xsl:attribute name="face">Courier</xsl:attribute>

          <xsl:attribute name="color">

            <xsl:value-of select="@color"/>

          </xsl:attribute>

          dog

        </font> said: "<xsl:value-of select="desc"/>"

        weight:<xsl:value-of select="@weight"/>

 

        <xsl:if test="@weight < 10">

          <p>

            <i>its weight is less than 10 km</i>

          </p>

        </xsl:if>

      </li>

    </xsl:for-each>

  </xsl:template>

 

 

 

  <xsl:template match="pig">

    <xsl:for-each select=".">

      <li>

        <img align="right">

          <xsl:attribute name="src">http://www.php.cn/;/xsl:attribute>

        </img>

        <font>

          <xsl:attribute name="face">Courier</xsl:attribute>

          <xsl:attribute name="color">

            <xsl:value-of select="@color"/>

          </xsl:attribute>

          pig

        </font> said: "<xsl:value-of select="desc"/>"

        weight:<xsl:value-of select="@weight"/>

 

        <xsl:if test="@weight < 10">

          <p>

            <i>its weight is less than 10 km</i>

          </p>

        </xsl:if>

      </li>

    </xsl:for-each>

  </xsl:template>

 

 

  <xsl:template match="cat">

    <xsl:for-each select=".">

      <li>

        <img align="right">

          <xsl:attribute name="src">http://www.php.cn/@N00.jpg?1143660418</xsl:attribute>

        </img>

        <font>

          <xsl:attribute name="face">Courier</xsl:attribute>

          <xsl:attribute name="color">

            <xsl:value-of select="@color"/>

          </xsl:attribute>

          cat

        </font> said: "<xsl:value-of select="desc"/>"

        weight:<xsl:value-of select="@weight"/>

 

        <xsl:if test="@weight < 10">

          <p>

            <i>its weight is less than 10 km</i>

          </p>

        </xsl:if>

      </li>

    </xsl:for-each>

  </xsl:template>

   

</xsl:stylesheet>

登录后复制

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Xml;

 

namespace UseXslt

{

    class Program

    {

        static void Main(string[] args)

        {

            //声明XslTransform类实例

            System.Xml.Xsl.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();

 

            string xsltFile = @"X:\about.net\System.Xml\example\pets.xsl";

            using (StreamReader rdr = new StreamReader(xsltFile))

            {

                using (XmlReader xmlRdr = XmlReader.Create(rdr))

                {

                    //载入xsl文件

                    trans.Load(xmlRdr);

                }

            }

            string inputFile = @"X:\about.net\System.Xml\example\pets.xml";

            string outputFile = @"X:\about.net\System.Xml\example\pets-out.htm";

            //转化源文件输出到输出文件outputFile

            trans.Transform(inputFile, outputFile);

        }

    }

}

登录后复制

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

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

以上就是XSLT语法—在.net中使用XSLT转换xml文档的示例代码详解的详细内容,更多请关注php中文网其它相关文章!

相关标签:
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号