CSBlog主题制作初步<一>--用XslCompiledTransform对象将XML数据输出为其它格式
2008-11-24 17:44 咒语 阅读(576) 评论(0) 编辑 收藏 举报先准备一个XML文件:
Code
<?xml version="1.0" encoding="utf-8"?>
<Links>
<Link Name="Google" Url="http://www.google.com" />
<Link Name="LoveCherry" Url="http://www.cnblogs.com/lovecherry" />
<Link Name="CSDN.Net" Url="http://www.csdn.net" />
<Link Name="博客园" Url="http://www.cnblogs.com/" />
<Link Name="污染的Blog" Url="http://blog.csdn.net/wuhuiran" />
<Link Name="玩野网" Url="http://www.playes.net/" />
<Link Name="AJ:酒吧" Url="http://www.aj98.com/" />
<Link Name="才子:才子手记" Url="http://blog.54caizi.com/blog/" />
<Link Name="DLL:新的希望" Url="http://www.byval.org/" />
<Link Name="心灵:木鸟原创" Url="http://www.psder.com/" />
<Link Name="含笑修行's blog" Url="http://blog.knoweb.cn/" />
<Link Name="天擎科技" Url="http://www.69sea.com" />
<Link Name="黑妹博客" Url="http://blog.cnfol.com/xielina82782114" />
<Link Name="Devms论坛" Url="http://bbs.devms.com" LogoUrl="/Images/Links/dmfourm.jpg" Title="Asp.Net Commitury." />
</Links>
<?xml version="1.0" encoding="utf-8"?>
<Links>
<Link Name="Google" Url="http://www.google.com" />
<Link Name="LoveCherry" Url="http://www.cnblogs.com/lovecherry" />
<Link Name="CSDN.Net" Url="http://www.csdn.net" />
<Link Name="博客园" Url="http://www.cnblogs.com/" />
<Link Name="污染的Blog" Url="http://blog.csdn.net/wuhuiran" />
<Link Name="玩野网" Url="http://www.playes.net/" />
<Link Name="AJ:酒吧" Url="http://www.aj98.com/" />
<Link Name="才子:才子手记" Url="http://blog.54caizi.com/blog/" />
<Link Name="DLL:新的希望" Url="http://www.byval.org/" />
<Link Name="心灵:木鸟原创" Url="http://www.psder.com/" />
<Link Name="含笑修行's blog" Url="http://blog.knoweb.cn/" />
<Link Name="天擎科技" Url="http://www.69sea.com" />
<Link Name="黑妹博客" Url="http://blog.cnfol.com/xielina82782114" />
<Link Name="Devms论坛" Url="http://bbs.devms.com" LogoUrl="/Images/Links/dmfourm.jpg" Title="Asp.Net Commitury." />
</Links>
再准备一个XSLT模板文件
Code
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="Links" select="/Links/Link" />
<xsl:output
method="xhtml"
version="1.0"
encoding="utf-8"
omit-xml-declaration="no"
indent="yes"
media-type="application/xhtml+xml"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>
<xsl:template match="/">
<xsl:call-template name="T.LinkPanel" />
</xsl:template>
<xsl:template name="T.LinkPanel">
<div>
<ol>
<xsl:for-each select="$Links">
<li><xsl:value-of select="@Name" /></li>
</xsl:for-each>
</ol>
</div>
</xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
>
<xsl:variable name="Links" select="/Links/Link" />
<xsl:output
method="xhtml"
version="1.0"
encoding="utf-8"
omit-xml-declaration="no"
indent="yes"
media-type="application/xhtml+xml"
doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
/>
<xsl:template match="/">
<xsl:call-template name="T.LinkPanel" />
</xsl:template>
<xsl:template name="T.LinkPanel">
<div>
<ol>
<xsl:for-each select="$Links">
<li><xsl:value-of select="@Name" /></li>
</xsl:for-each>
</ol>
</div>
</xsl:template>
</xsl:stylesheet>
然后就是C#转换代码:
Code
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(@"C:\Web\T.xslt");
xsl.Transform(@"C:\Web\Links.xml", @"C:\Web\Link.html");
XslCompiledTransform xsl = new XslCompiledTransform();
xsl.Load(@"C:\Web\T.xslt");
xsl.Transform(@"C:\Web\Links.xml", @"C:\Web\Link.html");
执行后的输出结果:
Code
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div>
<ol>
<li>Google</li>
<li>LoveCherry</li>
<li>CSDN.Net</li>
<li>博客园</li>
<li>污染的Blog</li>
<li>玩野网</li>
<li>AJ:酒吧</li>
<li>才子:才子手记</li>
<li>DLL:新的希望</li>
<li>心灵:木鸟原创</li>
<li>含笑修行's blog</li>
<li>天擎科技</li>
<li>黑妹博客</li>
<li>Devms论坛</li>
</ol>
</div>
怎么样?是不是超简单呀? 呵~~~ <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE div PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<div>
<ol>
<li>Google</li>
<li>LoveCherry</li>
<li>CSDN.Net</li>
<li>博客园</li>
<li>污染的Blog</li>
<li>玩野网</li>
<li>AJ:酒吧</li>
<li>才子:才子手记</li>
<li>DLL:新的希望</li>
<li>心灵:木鸟原创</li>
<li>含笑修行's blog</li>
<li>天擎科技</li>
<li>黑妹博客</li>
<li>Devms论坛</li>
</ol>
</div>
待续...