关闭顶部展开顶部

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

编辑Tag赚U币

推荐:解析.Net 4.0 中委托delegate的使用详解
本篇文章是对.Net 4.0 中委托delegate的使用进行了详细的分析介绍,需要的朋友参考下

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
复制代码 代码如下:www.mb5u.com

<?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”用来定义模板匹配的元素
如下定义匹配根节点的模板

复制代码 代码如下:www.mb5u.com

<xsl:template match=”/”>
</xsl:template>

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

<xsl:for-each select=”/pets/*”>
<xsl:value-of select=”name()”/>
</xsl:for-each>

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

<xsl:if test=”@weight < 10”>
<i>its weight is less than 10 km</i>
</xsl:if>

4) xsl:choose 多分支条件显示 (类似编程语言中的switch语句)
复制代码 代码如下:www.mb5u.com

<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
复制代码 代码如下:www.mb5u.com

<?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://estar-tv.com/images/comprofiler/gallery/dog.gif</xsl:attribute>
</xsl:when>
<xsl:when test="name() = 'pig'">
<xsl:attribute name="src">http://www.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</xsl:attribute>
</xsl:when>
<xsl:otherwise>
<xsl:attribute name="src">http://farm1.static.flickr.com/14/buddyicons/22211409@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:
复制代码 代码如下:www.mb5u.com

<?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://estar-tv.com/images/comprofiler/gallery/dog.gif</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.icosky.com/icon/thumbnails/Animal/Farm/Pig%20Icon.jpg</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://farm1.static.flickr.com/14/buddyicons/22211409@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也可以使用,但是这个类已经被微软标记为过时,最好不要再用了,如下代码示例:
复制代码 代码如下:www.mb5u.com

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还可以指定参数,定义变量,有关这些方面请查看相关文档。

分享:深入分析XmlSerializer对象的Xml序列化与反序列化的示例详解
本篇文章是对XmlSerializer 对象的Xml序列化与反序列化的应用进行了详细的分析介绍,需要的朋友参考下

来源:模板无忧//所属分类:.Net教程/更新时间:2013-05-19
loading.. 评论加载中....
相关.Net教程
闂備焦鐪归崺鍕垂闁秵鍋ら柡鍥舵緛缂嶆牠鏌涢埄鍐姇闁抽攱甯楅妵鍕即濡も偓娴滄儳顪冮妶蹇曠Ф闁瑰嚖鎷�
婵犵數鍋涢顓熸叏椤撱垹纾婚柟鐐灱濡插牓鏌¢崒婵囩《濠⒀勭⊕娣囧﹪鎳犻澶嗗亾濠靛鐓熼柕蹇婃櫅閻忔煡鏌熼崙銈嗗
婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掑嫷妫戠紒杈╁仜椤撳吋寰勭€n亝顓鹃柣搴f嚀鐎氼厽绔熼崱娆愬床濞戞柧绶氶弻锝夊閳轰胶浼堥梺鐟板殩閹凤拷
婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掑嫷妫戠紒杈╁仜椤撳吋寰勬繝鍕靛悑闂備浇顕栭崹搴ㄥ礋椤撗勑氶梻浣筋嚙闁帮絽岣胯铻炴繛鎴欏灪閸嬪鏌ㄩ悤鍌涘
闂傚倷鑳舵灙缂佺粯鍔欓獮濠冩償閵娿儳鍔﹀銈嗗笒閸婂摜鏁崼鏇炵閻庢稒岣块惌鎺斺偓瑙勬礃缁诲牓寮幘缁樻櫢闁跨噦鎷�
闂傚倷鑳舵灙缂佺粯鍔欓獮濠冩償閵娿儳鍔﹀銈嗗坊閸嬫捇鏌涘顒夊剳缂侇喖锕弫鍌炲礈瑜忛悡鎾绘煟鎼搭垳宀涢柡鍛箞閹苯鈻庨幋鐘碉紳婵炴挻鑹鹃敃銉р偓姘炬嫹
闂傚倷娴囬妴鈧柛瀣尰閵囧嫰寮介妸褉妲堥梺浼欏瘜閸o綁寮诲☉妯锋斀闁归偊浜為懗铏圭磽娴i潧濡芥俊鐐舵椤曪綁濡搁埡濠冩櫖濠电偛妫欓崹鐢电箔閿熺姵鈷戦柛婵嗗閳ь剙顭烽獮濠囧箻閼告娼熼梺璺ㄥ櫐閹凤拷
闂傚倷绀侀幖顐﹀疮閻楀牊鍙忓瀣捣缁€濠傤熆閼搁潧濮堥柣鎿勭秮閹娼幏宀婂妳濠电偛鐪伴崐鏇㈡箒闂佹寧绻傚Λ娆戠矆閳ь剟姊洪崨濞氭垿鎮ч悩鑼殾闁规壆澧楅弲鎼佹煥閻曞倹瀚�
濠电姷顣藉Σ鍛村垂椤忓牆绀堟繝闈涙-閻斿棙淇婇姘倯閻庢氨绮妵鍕箻鐎靛摜鐣洪柣搴㈢啲閹凤拷
婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掑嫷妫戠紒杈╁仜椤撳ジ宕堕妸銉紩婵$偑鍊栭幐鑽ゆ崲閸愵亖鍋撳铏彧濞e洤锕、娑樜旈埀顒佹櫠閸欏绡€妞ゎ偒鍠楃€氾拷
濠电姷顣藉Σ鍛村垂椤忓牆绀堟繝闈涙-閻斿棙鎱ㄥ璇蹭壕閻庤娲﹂崜鐔笺€佸▎鎾崇闁绘挸绨堕崑鎾绘偨閸涘﹦鍘搁梺鍓插亝缁诲嫬鐡┑鐘愁問閸n垶骞忛敓锟�
濠电姷顣藉Σ鍛村垂椤忓牆绀堟繝闈涙-閻斿棙鎱ㄥ璇蹭壕闂佸搫鑻幊姗€宕洪埀顒併亜閹哄棗浜鹃梺瀹狀嚙闁帮綁鐛幋锕€绠涢梻鍫熺⊕椤斿棝姊绘担鍛婃儓閻庢碍鎮傞獮蹇涙晸閿燂拷
濠电姷顣藉Σ鍛村垂椤忓牆绀堟繝闈涙-閻斿棙鎱ㄥ璇蹭壕閻庤娲╃紞渚€鐛€n亖鏀介柛顐亗缁憋拷
婵犵數濮烽。浠嬪焵椤掆偓閸熷潡鍩€椤掑嫷妫戠紒杈╁仜椤撳吋寰勬繝鍕幀闂備胶鎳撻悺銊ф崲閸曨垼鏁傞悗娑櫭肩换鍡樸亜閺嶎煈娈斿褍寮剁换婵嬶綖椤旇棄顏�
闂備浇顕ф鎼佸储濠婂牆绀堟繝闈涱儐閸嬪鏌熼幆鐗堫棄闁活厽顨呴埞鎴︽偐閹绘帗娈梺濂告涧缂嶅﹪寮婚妶澶嬪殟闁靛鍎遍~宥夋⒑缂佹ḿ澹勭紓宥勭閻e嘲鈻庨幘瀛樻闂佽法鍣﹂幏锟�
UB闂傚倷绀佸﹢杈╁垝椤栫偛绀夋俊銈呮噽瀹撲線鏌涢妷銏℃珕閻庢碍宀搁幃妤€鈽夊▍铏灴閿濈偤鏁撻敓锟�
闂傚倷娴囨竟鍫熴仈缁嬫娼栧┑鐘崇閻掗箖鎮楅悽鐢点€婇柛瀣崌閻涱噣宕归鍙ョ棯婵犵數濮崑鎾绘煥閻曞倹瀚�
闂備浇顕ф鍝ョ不瀹ュ鍨傞柛婵嗗閼板潡鏌涢姀銏犳
闂傚倷绀侀幉鈥愁潖缂佹ɑ鍙忓瀣捣缁€濠傤熆鐠鸿櫣鐏遍柛妤佺缁绘盯宕卞Δ鈧銏ゆ煕閻愬樊妲圭紒缁樼〒缁辨瑩鎮╅崫鍕腐闂備胶枪妤犲繘骞忛敓锟�
缂傚倸鍊搁崐鐑芥倿閿曞倹鍋¢柨鏇炲€搁崹鍌涚節婵犲倸顏い鈺呮敱閵囧嫰骞掑鍥舵М缂備焦褰冨﹢閬嶅焵椤掑倸浠柛濠冪箘缁辨挸顫濇0婵囨櫓闂佽法鍣﹂幏锟�
©2017 www.mb5u.com濠电姷顣藉Σ鍛村垂椤忓牆绀堟繝闈涙-閻斿棙鎱ㄥ璇蹭壕閻庢鍣崳锝夊箖閳哄懎绠甸柟鐑樻尭閻︼拷
闂傚倷娴囬妴鈧柛瀣崌閺岀喖顢涘⿰鍐炬毉濡炪們鍎婚幏锟�&闂傚倷绀侀幉锛勬暜閹烘嚦娑㈠籍閸噥妫呴梺璺ㄥ櫐閹凤拷