Tải bản đầy đủ (.pdf) (15 trang)

Thao tác XML - Sử dụng XPath và XSLT trong .NET – Phần 2 doc

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (109.78 KB, 15 trang )

Thao tác XML
Sử dụng XPath và XSLT trong .NET – Phần 2

Biến đổi XML
Ví dụ đầu tiên mà chúng ta xem xét lấy tài liệu books.xml và thể hiện dưới
dạng HTML ví dụ sử dụng file XSLT: books.xsl. (mã có thể tìm thấy trong
thư mục XPathXSLSample3.) Chúng ta phải thêm các dòng using sau:
using System.IO;
using System.Xml.Xsl;
using System.Xml.XPath;
Đây là mã thực thi:
private void button1_Click(object sender, System.EventArgs e)
{
//create the new XPathDocument
XPathDocument doc = new XPathDocument(" \\ \\ \\booksxpath.xml");
//create a new XslTransForm
XslTransform transForm = new XslTransform();
transForm.Load(" \\ \\ \\books.xsl");
//this FileStream will be our output
FileStream fs=new FileStream(" \\ \\ \\booklist.html",
FileMode.Create);
//Create the navigator
XPathNavigator nav = doc.CreateNavigator();
//Do the transform. The output file is created here
transForm.Transform(nav, null, fs);
}
Chúng ta tạo một đối tượng xuất phát từ XPathDocument và một đối tượng
xuất phát từ XslTransform. Load file booksxpath.xml vào XPathDocument,
và books.xsl vào XslTransform.
Trong ví dụ này, chúng ta cũng tạo một đối tượng FileStream để ghi tài liệu
HTML mới lên đĩa. Nếu đây là một ứng dụng ASP.NET, chúng ta có thể


dùng một đối tượng TextWriter và truyền nó cho đối tượng HttpResponse.
Nếu chúng ta đang thay đổi một tài liệu XML khác, chúng ta có thể dùng
một đối tượng XmlWriter.
Sau khi đã chuẩn bị các đối tượng XPathDocument và XslTransform, chúng
ta tạo XPathNavigator trên XPathDocument, và truyền XPathNavigator và
FileStream cho phương thức Transform() của đối tượng XslTransform.
Transform() có một vài quá tải, truyền và kến nối các điều hướng,
XsltArgumentList (trình bày sau), Và IO streams. Tham số đều hướng có thể
là XPathNavigator, hoặc bất kì đối tượng nào thực thi giao diện
IXPathNavigable. IO streams có thể là một TextWriter, Stream, hoặc một
đối tượng XmlWriter.
Tài liệu books.xsl giống như sau:
<xsl:stylesheet version="1.0"
xmlns:xsl="
<xsl:template match="/">
<html>
<head>
<title>Price List</title>
</head>
<body>
<table>
<xsl:apply-templates/>
</table>
</body>
</html>

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

<xsl:template match="book">
<tr><td>
<xsl:value-of select="title"/>
</td><td>
<xsl:value-of select="price"/>
</td></tr>
</xsl:template>
</xsl:stylesheet>
Sử dụng XsltArgumentList
Chúng ta đã sớm đề cập đến XsltArgumentList. Đây là cách bạn kết nối một
đối tượng với các phương thức và một không gian tên. Khi hoàn tất, bạn có
thẻ triệu gọi các phương thức trong quá trình biến đổi. Hãy xem ví dụ dưới
đây và chỉ ra cách hoạt động của chúng (mã có thể tìm thấy trong
XPathXSLSample4).
private void button1_Click(object sender, System.EventArgs e)
{
//new XPathDocument
XPathDocument doc=new XPathDocument(" \\ \\ \\booksxpath.xml");
//new XslTransform
XslTransform transForm=new XslTransform();
transForm.Load(" \\ \\ \\booksarg.xsl");
//new XmlTextWriter since we are creating a new XML document
XmlWriter xw=new XmlTextWriter(" \\ \\ \\argSample.xml",null);
//create the XsltArgumentList and new BookUtils object
XsltArgumentList argBook=new XsltArgumentList();
BookUtils bu=new BookUtils();
//this tells the argumentlist about BookUtils
argBook.AddExtensionObject("urn:ProCSharp",bu);
//new XPathNavigator
XPathNavigator nav=doc.CreateNavigator();

//do the transform
transForm.Transform(nav,argBook,xw);
xw.Close();
}
//simple test class
public class BookUtils
{
public BookUtils(){}

public string ShowText()
{
return "This came from the ShowText method!";
}
}
Đây là file đã biến đổi (argSample.xml):
<?xml version="1.0"?>
<books>
<discbook>
<booktitle>The Autobiography of Benjamin Franklin</booktitle>
<showtext>This came from the ShowText method!</showtext>
</discbook>
<discbook>
<booktitle>The Confidence Man</booktitle>
<showtext>This came from the ShowText method!</showtext>
</discbook>
<discbook>
<booktitle>The Gorgias</booktitle>
<showtext>This came from the ShowText method!</showtext>
</discbook>
<discbook>

<booktitle>The Great Cookie Caper</booktitle>
<showtext>This came from the ShowText method!</showtext>
</discbook>
<discbook>
<booktitle>A Really Great Book</booktitle>
<showtext>This came from the ShowText method!</showtext>
</discbook>
</books>
Trong ví dụ này, chúng ta định nghĩa một lớp mới, BookUtils. Trong lớp này
chúng ta có một phương thức chỉ trả về chuỗi "This came from the
ShowText method!". Trong sự kiện button1_Click(), chúng ta tạo ra
XPathDocument và XslTransform như chúng ta đã từng làm với hai ngoại
lệ. Trong lúc chúng ta tạo một tài liệu XML, vì thế chúng ta dùng XmlWriter
thay cho FileStream mà chúng ta đã từng dùng. Các thay đổi khác:
XsltArgumentList argBook=new XsltArgumentList();
BookUtils bu=new BookUtils();
argBook.AddExtensionObject("urn:ProCSharp",bu);
Chúng ta tạo một XsltArgumentList. Chúng ta tạo một thể hiện của đối
tượng BookUtils,và gọi phương thức AddExtensionObject(). Khi gọi
Transform(), chúng ta truyền trong các đối tượng XsltArgumentList
(argBook) với XPathNavigator và XmlWriter mà chúng ta đã tạo.
Đây là tài liệu booksarg.xsl:
<xsl:stylesheet version="1.0"
xmlns:xsl="
xmlns:bookUtil="urn:ProCSharp">
<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">
<xsl:element name="books">
<xsl:apply-templates/>

</xsl:element>
</xsl:template>
<xsl:template match="bookstore">
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<xsl:element name="discbook">
<xsl:element name="booktitle">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="showtext">
<xsl:value-of select="bookUtil:ShowText()"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Hai dòng quan trọng được in đậm. Đầu tiên chúng ta thêm không gian tên
mà chúng ta đã tạo khi thêm đối tượng XsltArgumentList. Sau đó khi muốn
gọi, chúng ta sử dụng không gian các kí hiệu chuẩn trong không gian tên
XSLT .
Chúng ta còn có một cách khác đó là sử dụng XSLT scripting. Bạn có thể
nhúng mã C#, VB, và JavaScript trong một stylesheet. Không giống như
thực thi phi .NET kịch bản sẽ được thực thi khi XslTransform.Load() được
gọi; cách này thực thi các kịch bản đã được biên dịch, giống như cách mà
ASP.NET làm.
Hãy sửa đổi file XSLT trên theo cách này. Trước tiên chúng ta thêm kịch
bản vào stylesheet. bạn có thể thấy các thay đổi trong booksscript.xsl dưới
đây:
<xsl:stylesheet version="1.0"
xmlns:xsl="

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="">

<msxsl:script language="C#" implements-prefix="user">

string ShowText()
{
return "This came from the ShowText method!";

}
</msxsl:script>

<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<xsl:element name="books">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="bookstore">
<xsl:apply-templates select="book"/>
</xsl:template>
<xsl:template match="book">
<xsl:element name="discbook">
<xsl:element name="booktitle">
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="showtext">
<xsl:value-of select="user:ShowText()"/>
</xsl:element>
</xsl:element>

</xsl:template>
</xsl:stylesheet>
Chúng ta cài không gian tên scripting, và thêm mã (đã được biên dịch bởi
Visual Studio .NET IDE), và tạo một lời gọi đến stylesheet. File xuất giống
hệt ví dụ trên.
Tóm lại, sử dụng XPathDocument nếu bạn muốn soạn thảo,
XmlDataDocument nếu bạn muốn lấy dữ liệu từ ADO.NET, và
XmlDocument nếu bạn muốn có thể thao tác dữ liệu.

×