Tạo HTML từ XML và XSLThông thường, nếu bạn định nghĩa bên trong một XML file rằng nó cần một XSL để display thì Internet Exporer 5.5 sẽ tự transform XML dựa theo XSL và display kết quả
Trang 1Tạo HTML từ XML và XSL
Thông thường, nếu bạn định nghĩa bên trong một XML file rằng nó cần một XSL để display thì Internet Exporer 5.5 sẽ tự transform XML dựa theo XSL và display kết quả trong browser cho bạn Trong trường hợp ấy nếu bạn View Source của Webpage bạn chỉ đọc đuợc XML source mà thôi Có thể bạn sẽ thắc mắc sao không thấy cái HTML source là kết quả của quá
trình Tranform XML.
Trong chương trình VB6 trình bày tại đây bạn sẽ thấy cách dùng DOM
(Document Object Model) của MSXML để thực hiện công việc tương
đương với IE 5.5 và save kết quả HTML thành một Webpage Webpage nầy
có thể được display, độc lập với XML, bằng bất cứ WebBrowser nào.Thật ra, ActiveX MSXML mà ta dùng cho VB6 là của IE 5.5 khi ta Project |
Reference "Microsoft XML, v3.0".
Chương trình mẫu
Bạn có thể download chương trình mẫu TransformXML.zip để xem cách thảo chương rất đơn giản
Trang 2Phần chính của chương trình nằm trong Sub Form_Load như liệt ra dưới đây
Bạn có thể xem listing và giải thích về cách transform Library.xml dựa trên Library.xsltrong bài XML, Kỹ thuật tin học nòng cốt trong tương lai
Private Sub Form_Load()
Dim HTMLCode As String
Dim myXMLDoc As New MSXML2.DOMDocument30
Dim myXSLDoc As New MSXML2.DOMDocument30
Dim Fs As FileSystemObject
Dim TS As TextStream
PopulateListBoxFromFile LstXML, "Library.xml", False
PopulateListBoxFromFile lstXSL, "Library.xsl", False
myXMLDoc.Load App.Path & "\Library.xml"
myXSLDoc.Load App.Path & "\Library.xsl"
Trang 3HTMLCode = myXMLDoc.transformNode(myXSLDoc)
Set Fs = CreateObject("Scripting.FileSystemObject")
Set TS = Fs.OpenTextFile(App.Path & "\Library.htm", ForWriting, False, TristateUseDefault) TS.Write HTMLCode ' Write the whole HTML string in one stroke
TS.Close ' Close the Text Stream
Set Fs = Nothing ' Dispose FileSystem Object
PopulateListBoxFromFile lstHTML, "Library.htm", False
End Sub