So the first question is why would we use an Domino XML Data Source instead of a standard view design? I'm glad you asked. A couple of reasons.
Views can be limited to the amount of data that they display in the alloted columns. Using Java and LotusScript developers can write agents that returned well formed XML that can be consumed by both Domino and non-Domino systems.
There is currently an issue with XPages consuming web services when the XPage and the Web Service is hosted on the same Domino Server. If hosting the Web Service and the XPage on separate servers is not an option, then the Web Service logic could be coded as an agent that returns well formed XML that could then be consumed by any system, Domino and non-Domino alike.
Back to the issue at hand. In this article I will demonstrate two ways to display XML data as a data source on an XPage.
- Via XSLT
- Via an XPage Data Table
Transforming XML on an XPage via XSLT
So that it is easy for you to duplicate the examples I will be providing I'm going to use names.nsf (local or server) as the data source. This way you can test the example code with "real" data at your house
The following image demonstrates the output of reading an XML data stream, transforming it on the fly with XSLT and displaying the results (there is nothing wrong with your eyes, I just blurred the details as my mom didn't want her email published). The name of the XPage is XPageWithTransfomedDominoXML.xsp in the XML Playground application you can download from http://www.nnsu.com
The output is produced by running an agent called javanames.xml and applying an XSLT called names.xsl
The Pros of this solution are
- Works well with small data sets
- Results are dynamic
- All document data is available, not just the view data
The Cons to this solution are
- Not easy to apply paging
- All formatting has to be done via CSS added to the transformed output (not a huge con, if you use css classes)
- Must know how to write an XSLT transformation stylesheet.
If that last bullet point applys to you then your question is "Paul, can't I produce these same results via standard XPage controls?" and the answer to the question is yes you can !
Displaying XML data on an XPage using a Data Table control
The following image demonstrates the output of reading an XML data stream, parsing it with the Java 6 XML parser and displaying the results in a standard XPage Data Table control. The name of the XPage is XPageWithXMLDataSource.xsp in the XML Playground application you can download from here.
This technique is accomplished using the following procedure
- Parse the XML and store it to a session scoped variable in the XPages afterPageLoad event
- Add a Data Table control to the XPage
- Add columns to the Data Table for each element of the XML to be displayed
- Add computed field controls to the Data Table columns to display the parsed XML
- Add a pager control to the Data Table for paging purposes
The XPage in design mode looks like the following;
Create the XML Document
var parsedxml:org.w3c.dom.Document = null;
Create the Parser Factory and document builder
var domfactory:javax.xml.parsers.DocumentBuilderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
var xmldocument:javax.xml.parsers.DocumentBuilder = domfactory.newDocumentBuilder();
Read the XML from the Domino Agent
var parsedxml = xmldocument.parse("http://localhost/dotoxp.nsf/javanames.xml");
Store the parsed XML to a session scope variable.
Add a Data Table control to the XPage with the following properties. Notice the "Index name:" variable as it will play an important part in the column formulas.
The Iteration formula (determines how many elements the data table displays) the following Server Side Java Script is used to read the parsed XML variable, read the fullname node list and return the elements of that node list.
Read the parsed XML from the Session scope variable
var domdoc:DOMDocument = sessionScope.get("names");
Create a node list of all of the fullName elements from the XML Stream
var dtnames:DOMNodeList = domdoc.getElementsByTagName("fullname");
Return the entire node list
The Data Table control includes a repeat control that repeats the rows for every element of the data source. Add columns to the Data Table control for each element of the XML stream to be displayed
Pros of this technique
Any XML data source that can be read by URL (even non-Domino data sources) can now be used as data sources on an XPage
Any of the XML "nodes" can be displayed (all or one)
Pager control can be added to the Data Table Control to "page" through the data
CSS formatting can be applied to XPage controls via the designer UI
Cons of this technique
So now you can start using any accessible XML data stream in your XPage development.
Don't forget you can download the complete XML Playground application with the included XPage solutions at http://www.nnsu.com