As I mentioned in my first article I have been a huge fan of XML and related technologies since XML first came on the scene. So any time I have an opportunity to incorporate XML into my development and it meets a particular need, then I get pretty excited.
So in searching all of the excellent resources on XPages I hadn't yet come across a technique for transforming XML on the fly with an XSLT stylesheet and displaying the results in an XPage. This technique will allow you to use any XML data source (via URL) and transform it with a stylesheet stored in the Domino application and display the results anywhere on your XPage. So a picture being worth a thousand words the result would resemble the following.

In this particular case I am reading the XPages Blog RSS feed and transforming it into HTML links via XSLT and then rendering the results in the XPage inside of a Panel control.
So here is the walk through.
Design Elements
- URL of XML (In this case it can be any web sites RSS feed URL)
- URL of XSLT (Stylesheet included in the sample application)
- XPage with Computed Field using Server Side JavaScript
Create a new XPage or edit an existing page. Add a computed field to the page. My design looks like the following

On the Value tab of the Computed Field controls Properties tab, change the Bind data using to JavaScript. Click on the Open Script Dialog icon to open the Script editor to add the Server Side JavaScript code.

The following is the Server Side JavaScript code that reads the XML RSS Feed, reads the XSLT stylesheet, creates the result object, performs the transformation and renders the results onto the XPage.
var xmlSource:javax.xml.transform.Source = javax.xml.transform.stream.StreamSource("http://xpagesblog.com/xpages-blog/rss.xml" );
var xsltSource:javax.xml.transform.Source = javax.xml.transform.stream.StreamSource("http://localhost/XPagePlay.nsf/newsstyle.xsl" );
var result:javax.xml.transform.Result = javax.xml.transform.stream.StreamResult(facesContext
.getResponseWriter());
// create an instance of TransformerFactory
var
transFact:javax.xml.transform.TransformerFactory = javax.xml.transform.TransformerFactory.newInstance( );
var
trans:javax.xml.transform.Transformer = transFact.newTransformer(xsltSource);
//return result;
trans.transform(xmlSource, result);
Thats all there is to it.
This technique takes advantage of the fact the during XPage development we have complete access to the entire Java 6 JDK, which includes the XML parsers and transformation engine.
The first line of code reads the RSS XML. Note that this could be ANY xml. This example is using RSS because it is readily available for demonstration purposes, but this could be any XML from an accessible web site including a Java or Lotus Script agent on a Domino Server.
The second line of code gets the XSLT stylesheet. Now the Style sheet is specific to the XML source that is being transformed. In this case the referenced stylesheet will ONLY transform RSS feeds. But you could write XSLT stylesheets to transform whatever XML you are consuming.
The third line of code creates the transformed results and sets the output to the XPages responseWriter.
Next we create an instance of the transformation factory.
Finally the transformation is performed with the results being written to the XPage.
Using this technique developers can include Domino and non-Domino data sources represented as XML and transformed on the fly to be rendered in the XPage.
I have included an application (XPages Playground) with the complete solution on my web site.
In my next post I will show how to use raw XML as a data source and output the results to the XPage.