resents your XML AND WEB SERVICES E-NEWSLETTER for December 8, 2003 <-------------------------------------------> USE XSLTRANSFORM TO TRANSFORM XML INTO HTML The .NET Framework is centrally focused around one main premise: XML. This impacts everything from DataSets used in ADO.NET to .NET Web services. Therefore, it's not surprising to find a vast array of Framework classes that deal with various aspects of XML. One class in particular, XslTransform, provides a powerful foundation for automatically transforming XML data into presentable HTML syntax. Here are some of the features and functionality of this class, which will help you build a Web service that's capable of rendering HTML content to its consumers. MAIN FUNCTIONALITY The XslTransform class has one main, simple purpose: to render an XSL document fed by an XML data source. Things become complicated when you consider the methods used to feed the information to this class. For example, XslTransform's Load() method has eight constructors to accept a variety of different input sources for the XSL style sheet. The Transform() method has nine overloaded methods that you may use to render the XSL style sheet. EXAMPLE SERVICE This Web service example may seem peculiar, but it's very versatile and generic. Here's how to create a Web service that loads a set of XML data, loads an XSLT file that is fed by the XML data, and returns a byte array of HTML content that may be displayed in any ASPX page. LISTING A shows the VB.NET code required for the Web method: Listing A Public Function GetHtml() As Byte() ' Instantiate a DataSet to hold the XML data Dim Ds As New DataSet() Ds.ReadXml("physical path to XML file") ' Instantiate an XslTransform class to perform the XML transformation Dim Transform As New Xsl.XslTransform() Transform.Load("physical path to xslt file") ' Read the DataSet into an XmlDocument object Dim XMLDoc As New XmlDocument() XMLDoc.LoadXml(Ds.GetXml()) ' Generate an XPathNavigator object from the Xml Document Dim XNav As XPathNavigator = XMLDoc.CreateNavigator() ' Create a Stream object to contain the binary output from the transformation process Dim Stream As New MemoryStream() Transform.Transform(XNav, Nothing, Stream) ' Return the byte array content to the consumer Return Stream.ToArray() End Function This method has been programmed specifically to use objects that are more flexible than physical paths. For example, the Transform() method can be called by passing in physical path references to the XML data store and the resulting XSL output file. By using a dataset to load the XML file, you can reuse this code to have your dataset loaded from a database table instead of a physical file. LISTING B shows the content of the XML file that was used for this example: Listing B John Doe
1234 Somewhere Street
Los Angeles California 000-555-0000 000-555-1111
The XML document simply contains a typical set of personal profile information that you may find in an Outlook Contact entry. If the data were loaded from a database table, the structure could be dynamic and have any number of different XML elements. The XSL file is the key to the entire transformation process. The XSL file contains the HTML and XSL tags that dictate how the XML data will be presented. You can think of the XSL file itself as a template that is populated by XML data. LISTING C shows the XSL file as used in this example: Listing C *
Firstname:
Lastname:
Address:
City:
State:
Phone:
Fax:
The XSL file in this example is a simple HTML document with some tags that indicate where to place certain XML data. When this XSL is transformed by the XslTransform class, the XML document is parsed and the select="?" portion is matched to an XML element in the XML data source. If a match is found, the XML data is placed where the tag resides. This is a very similar mechanism to inline scripting in ASPX pages. Add an ASPX page to your test project and add the code in LISTING D to invoke the Web service: Listing D * Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim Obj As New HtmlWS() Dim Content() As Byte = Obj.GetHtml() Response.BinaryWrite(Content) End Sub The Web service is instantiated, and the GetHtml() method is invoked. The resulting byte array is then fed directly into the Response object. Because the content of the byte array returned from the Web service is HTML, the ASPX page will display the rendered content from the XslTransform class. This is what you should see: Firstname: John Lastname: Doe Address: 1234 Somewhere Street City: Los Angeles State: California Phone: 000-555-0000 Fax: 000-555-1111 STANDARD XML PATTERN When architecting your Web services, you must employ reusability and OOP methodology. Even though the example is written in .NET, the purpose behind Web services is interoperability. For instance, your consumer may be a Java application. By expanding on this principle, you may design a document management system that utilizes all of the most widely adopted standards: HTML, XML, and XSL. By being able to feed an XSL template a dynamic XML data source, you could implement an extremely powerful reporting mechanism, allowing consumers to draw data from your Web services into an HTML format. Kevin Koch is a senior software engineer with extensive experience in both Microsoft's .NET platform and J2EE technologies. He is a cofounder and president of Task Solutions Inc. ----------------------------------------