zoukankan      html  css  js  c++  java
  • 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

    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>this is a blue pig</desc>
    07   </pig>
    08   <cat color="red" weight="9">
    09     <price>80</price>
    10     <desc>this is a red cat</desc>
    11   </cat>
    12   <dog color="green" weight="15">
    13     <price>80</price>
    14     <desc>this is a green dog</desc>
    15   </dog>
    16   <cat color="green" weight="15">
    17     <price>80</price>
    18     <desc>this is a green cat</desc>
    19   </cat>
    20   
    21   
    22   <dog color="blue" weight="10">
    23     <price>100</price>
    24     <desc>this is a blue dog</desc>
    25   </dog>
    26   <dog color="red" weight="9">
    27     <price>80</price>
    28     <desc>this is a red dog</desc>
    29   </dog>
    30 </pets>

    上面的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
    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>lovely pets</title>
    09         <style type="text/css">
    10           ul{margin:10px 0 10px 0;padding:0;400px;text-align:left;}
    11           li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}
    12         </style>
    13       </head>
    14       <body>
    15         <center>
    16         <h1>lovely pets</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>its weight is less than 10 km</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:

    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>lovely pets</title>
    009         <style type="text/css">
    010           ul{margin:10px 0 10px 0;padding:0;400px;text-align:left;}
    011           li{height:60px;display:block;list-style:none;padding:4px;border:1px solid #f0f0f0;margin:5px;}
    012         </style>
    013       </head>
    014       <body>
    015         <center>
    016           <h1>lovely pets</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>its weight is less than 10 km</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>its weight is less than 10 km</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>its weight is less than 10 km</i>
    100           </p>
    101         </xsl:if>
    102       </li>
    103     </xsl:for-each>
    104   </xsl:template>
    105     
    106 </xsl:stylesheet>

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

    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.XslCompiledTransform trans = new System.Xml.Xsl.XslCompiledTransform();
    16   
    17             string xsltFile = @"X:\about.net\System.Xml\example\pets.xsl";
    18             using (StreamReader rdr = new StreamReader(xsltFile))
    19             {
    20                 using (XmlReader xmlRdr = 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标签 <META http-equiv="Content-Type" content="text/html; charset=utf-8">;微软帮我们想的太多了。

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

    C#处理Xml的相关随笔:


    请尊重作者的劳动,转载请保留链接 玉开的技术博客

  • 相关阅读:
    Linux shell 常用命令记录
    [Bat]批量重命名文件
    【转】java术语(PO/POJO/VO/BO/DAO/DTO)
    TesseractOCR3.0语言库训练步骤
    关于Delphi Rtti对应TDatetime的一些启示
    Ubuntu Server 12.04 安装mysql 5.6.10
    Delphi透明 圆角 窗体 【转】
    奇怪的~用法
    很有用的VS2005插件——SlickEdit
    还不习惯Office 2007
  • 原文地址:https://www.cnblogs.com/luluping/p/2117729.html
Copyright © 2011-2022 走看看