栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

解析在.net中使用XSLT转换xml文档的示例详解

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

解析在.net中使用XSLT转换xml文档的示例详解

XSL即可扩展的样式表文件。 可以格式化xml的显示,也可以将xml转换成需要的另一种格式。
学习XSL必须熟悉XPath。XSL和XPath一样简单强大,容易学习。
1. XSL既然可以格式化xml的显示样式,我们先来看如何在xml中引用xsl文件
如下代码示例:


只需在xml文件的文档声明后面添加即可
2. XSL的格式
XSL也是一个标准的xml文件,它以xml文档声明开始,根元素必须是xsl:styleshee,同时根元素必须有version属性指定xsl的版本,和xmlns:xsl=” http://www.w3.org/1999/XSL/Transform”指定xsl命名空间,如下示例


3. Xsl要点 如下示例xml
复制代码 代码如下:



 
    100
    this is a blue pig
 

 
    80
    this is a red cat
 

 
    80
    this is a green dog
 

 
    80
    this is a green cat
 


 
 
    100
    this is a blue dog
 

 
    80
    this is a red dog
 



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


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



2) xsl:for-each循环显示select=”xpath expression”选择节点的转换 (类似编程语言中的foreach语句),
如下示例,选择了pets下面的子元素,并循环显示子元素的几点名字:
复制代码 代码如下:

选择属性weight


将输出
7) xsl:apply-templates 应用模板
如果xml文件结构比较复杂,可以定义多个template,然后使用标签应用模板,xsl:apply-templates 可以指定属性select=”xpath”来选择应用的模板,或者不指定select表示选择当前节点的模板。
请看下面示例xslt文件pets-templates.xsl
完整的示例xsl文件:pets.xsl
复制代码 代码如下:

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
   
     
       
        lovely pets
       
          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;}
       
     
     
       


        lovely pets
       

             
               

  •              
                   
                     
                        http://estar-tv.com/images/comprofiler/gallery/dog.gif
                     

                     
                        http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg
                     

                     
                        http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418
                     

                   

                 
                 
                    Courier
                   
                     
                   

                   
                 
    said: ""
                  weight:

                 
                   


                      its weight is less than 10 km
                   


                 


     
               

  •          

           

       

     
   
 


完整示例文件 pets-templates.xsl:
复制代码 代码如下:

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 
   
     
       
        lovely pets
       
          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;}
       
     
     
       

          lovely pets
         

               
             

       

     
   
 


    
   
   
   
 


 
   
     

  •        
              http://estar-tv.com/images/comprofiler/gallery/dog.gif        
           
           
              Courier
             
               
             

              dog
           
    said: ""
            weight:

           
             


                its weight is less than 10 km
             


           

         

  •    

     


     

     
       
         

  •        
              http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg
           
           
              Courier
             
               
             

              pig
           
    said: ""
            weight:

           
             


                its weight is less than 10 km
             


           

         

  •    

     


     
     
       
         

  •        
              http://farm1.static.flickr.com/14/buddyicons/22211409@N00.jpg?1143660418
           
           
              Courier
             
               
             

              cat
           
    said: ""
            weight:

           
             


                its weight is less than 10 km
             


           

         

  •    

     




    在c#.net中使用XslCompiledTransform转换xml文档,XslTransform也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:
    复制代码 代码如下:
    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.netSystem.Xmlexamplepets.xsl";
                using (StreamReader rdr = new StreamReader(xsltFile))
                {
                    using (XmlReader xmlRdr = XmlReader.Create(rdr))
                    {
                        //载入xsl文件
                        trans.Load(xmlRdr);
                    }
                }
                string inputFile = @"X:about.netSystem.Xmlexamplepets.xml";
                string outputFile = @"X:about.netSystem.Xmlexamplepets-out.htm";
                //转化源文件输出到输出文件outputFile
                trans.Transform(inputFile, outputFile);
            }
        }
    }

    有一点需要注意,使用XslCompiledTransform转换出来的文件,是一个html格式的,这个类会自动在html的head标签中添加一个未关闭的meta标签 ;微软帮我们想的太多了。
    Xslt还可以指定参数,定义变量,有关这些方面请查看相关文档。

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/57695.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号