Understanding XSLT transformations: Your Own `XML Transformation Utility` - Understanding the XML transformation utility
(Page 5 of 6 )
Let me explain the above code part by part:
' Load the style sheet.
Dim xslt As New XslCompiledTransform()
xslt.Load(Me.txtXSLFilePath.Text)
The above statement creates an “XslCompiledTransform” object called “xslt”. It has a method “Load” which can load an existing XSL file. The second statement (in the above code) does the same. Further proceeding, we have the following:
' Create the writer.
Dim settings As New Xml.XmlWriterSettings()
settings.Indent = True
settings.IndentChars = vbTab
As we are transforming an XML document to HTML (which again looks very similar to XML syntax and structure), I would like to have the output shown along with proper “indentation”. To transform with proper “indentation” we need to specify some “settings” using the class “XmlWriterSettings”.
And thus I created an object (“settings’) based on the class “XmlWriterSettings” and finally specified the proper indentation settings. Further proceeding we have:
Dim sw As New IO.StringWriter
Dim writer As XmlWriter = XmlWriter.Create(sw, settings)
The transformation is something like a process of rewriting. And thus, I need to work with a “writer” type of class. “XmlWriter” is already an existing class, which is purely for this scenario. For my flexibility, I created an object “writer” by executing the method “XmlWriter.Create” along with an object related to “StringWriter” and our “indentation” settings. Further proceeding we have the following:
' Execute the transformation.
xslt.Transform(Me.txtXMLFilePath.Text, writer)
writer.Close()
Me.txtTransformResult.Text = sw.ToString
sw.Close()
The entire process takes place with only the first statement (in the above code fragment). After successfully transforming the XML file, we close and release resources and finally display the result back to a multi-lined textbox.
Next: Testing further with different types of XML >>
More XML Articles
More By Jagadish Chaterjee