Transforming XML Using XSLT and ASP.NET

only for RuBoard

When designing this application, I was faced with how I would automatically render the correct UI for each client that I aimed to support (either XHTML Basic or WML).

To facilitate this, I came up with some logic that allowed me to detect the browser type being used and to select the correct .xsl translation file and render it to the client device.

ASP.NET provides various classes that enable you to load existing XML documents into memory for transformation by an XSL file.

Listing 13.6 demonstrates a simple XML translation process by using ASP.NET.

Listing 13.6 xmlwmltext.aspx
 <%@ Import Namespace="System.Text" %>  <%@ Import Namespace="System.Xml" %>  <%@ Import Namespace="System.Xml.Xsl" %>  <script language="c#" runat="server">  private void Page_Load(object sender, System.EventArgs e)  { XmlDocument xmlDoc = new XmlDocument();  XslTransform xslDoc = new XslTransform();  XmlDocument worker = new XmlDocument();  // Load the source xml  xmlDoc.Load(Server.MapPath("polls.xml"));  // load the source xsl  xslDoc.Load(Server.MapPath("pollsWML.xsl"));  // Transform the xmlDoc, using xslDoc and load results into the worker object  worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(),  null));  // Print the result to the output stream  Response.ContentType = "text/vnd.wap.wml";  Response.Write(worker.InnerXml);  }  </script> 

Listing 13.6 shows an .aspx file used to load the polls.xml file, and applies an .xsl translation to it (stored in the pollsWML.xsl file). Finally, the results are rendered onscreen.

The first three lines of code declare the namespaces required for this example:

 <%@ Import Namespace="System.Text" %>  <%@ Import Namespace="System.Xml" %>  <%@ Import Namespace="System.Xml.Xsl" %> 

Next, a script block is declared to run server-side and to use the language of C#:

 <script language="c#" runat="server"> 

The page load event is then defined and three objects are defined for use:

  • xmlDoc ” Holds the XML document to process

  • xslDoc ” Holds the XSL transformation file to use

  • worker ” An XML document object that's used to hold the transformed XML document

 private void Page_Load(object sender, System.EventArgs e)  { XmlDocument xmlDoc = new XmlDocument();  XslTransform xslDoc = new XslTransform();  XmlDocument worker = new XmlDocument(); 

Next, the polls.xml file is loaded and stored by the xmlDoc object, as shown here:

 // Load the source xml  xmlDoc.Load(Server.MapPath("polls.xml")); 

Then we load and store the pollsWML.xsl XSL file we going to use for the transform in the xslDoc object

 // load the source xsl  xslDoc.Load(Server.MapPath("pollsWML.xsl")); 

Next, the actual transformation of the xmlDoc is performed by using the xslDoc object. The result is stored in the worker object:

 // Transform the xmlDoc, using xslDoc and load results into the worker object  worker.Load(xslDoc.Transform(xmlDoc.DocumentElement.CreateNavigator(),null)); 

Finally we set the return content type to text/vnd.wap.wml to make sure content is delivered in a WAP/WML friendly format, and then we finally write the results back to the client device. Before closing the function and a script block.

 // Print the result to the output stream  Response.ContentType = "text/vnd.wap.wml";  Response.Write(worker.InnerXml);  }  </script> 
only for RuBoard


XML and ASP. NET
XML and ASP.NET
ISBN: B000H2MXOM
EAN: N/A
Year: 2005
Pages: 184

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net