XSLT



设计XSLT(Extensible Stylesheet Language Transformation)的目的是将一个XML文档转换成不同格式或结构的另外一种文档。例如,将XML转换成一个网站使用的HTML形式,或转换成一个仅仅包含应用程序所需要的几个域的一种文档。这种转换操作基于W3C XSL Transformation(XSLT)1.0,可以查看http://www.w3.org/TR/xslt。在Microsoft.Net中,XslTransform是实现了这个功能的类。下面这个图也许可以说明Microsoft.Net中XSLT的框架:

 

 

这个XSLT推荐使用XPATH去选择一个XML文件中的内容,而XPATH相当与XML中的查询语言,在Microsoft.Net中实现了XPATH去选择存储的类有:XmlDocument,XmlDataDocument和XpathDocument,。而XpathDocument是最优的XSLT数据存储,它能提供XSLT转换很好的性能。

下面我们就以XpathDocument来说明如何使用XslTransform。

我们写一个Output.xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="bookstore">

<HTML>

<BODY>

<TABLE BORDER="2">

<TR>

<TD>ISBN</TD>

<TD>Title</TD>

<TD>Price</TD>

</TR>

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

</TABLE>

</BODY>

</HTML>

</xsl:template>

<xsl:template match="book">

<TR>

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

<TD><xsl:value-of select="title"/></TD>

<TD><xsl:value-of select="price"/></TD>

</TR>

</xsl:template>

</xsl:stylesheet>

其中定义了ISBN,Title和Price三个域,其中XSLT版本是1.0,匹配的是bookstore。而http://www.w3.org/1999/XSL/Transform是XSLT必须包括的名字空间。

现在我们根据这个Output.xsl来转换books.xml:

<?xml version='1.0'?>

<!-- This file represents a fragment of a book store inventory database -->

<bookstore>

<book genre="autobiography" publicationdate="1981" ISBN="1-861003-11-0">

<title>The Autobiography of Benjamin Franklin</title>

<author>

<first-name>Benjamin</first-name>

<last-name>Franklin</last-name>

</author>

<price>8.99</price>

</book>

<book genre="novel" publicationdate="1967" ISBN="0-201-63361-2">

<title>The Confidence Man</title>

<author>

<first-name>Herman</first-name>

<last-name>Melville</last-name>

</author>

<price>11.99</price>

</book>

<book genre="philosophy" publicationdate="1991" ISBN="1-861001-57-6">

<title>The Gorgias</title>

<author>

<name>Plato</name>

</author>

<price>9.99</price>

</book>

</bookstore>

我们在写一个test.cs来转换:

using System;

using System.IO;

using System.Xml;

using System.Xml.Xsl;

using System.Xml.XPath;

public class test

{

private const String filename = "books.xml";

private const String stylesheet = "output.xsl";

public static void Main()

{

XPathDocument myxd = new XPathDocument(stylesheet);

XPathNavigator myxn = myxd.CreateNavigator();

XslTransform xslt = new XslTransform();

xslt.Load(myxn);

XPathDocument doc = new XPathDocument(filename);

XmlTextWriter writer = new XmlTextWriter(Console.Out);//结果输出到Dos下。

xslt.Transform(doc, null, writer);

writer.Close();

}

}

运行这个程序,结果如下:

<HTML>

<BODY>

<TABLE BORDER="2">

<TR>

<TD>ISBN</TD>

<TD>Title</TD>

<TD>Price</TD>

</TR>

<TR>

<TD>1-861003-11-0</TD>

<TD>The Autobiography of Benjamin Franklin</TD>

<TD>8.99</TD>

</TR>

<TR>

<TD>0-201-63361-2</TD>

<TD>The Confidence Man</TD>

<TD>11.99</TD>

</TR>

<TR>

<TD>1-861001-57-6</TD>

<TD>The Gorgias</TD>

<TD>9.99</TD>

</TR>

</TABLE>

</BODY>

</HTML>

将它们存储成test.html,在浏览器显示如下:

 

可以看到一个XML文档以成功转换成一个HTML文件形式,而且里面数据没有丢失。

posted @ 2006-12-13 16:05  刘杨兵  阅读(352)  评论(1编辑  收藏  举报