Transforming XML

VERSION 1 Published

Created on:Jul 9, 2008 8:44 AM by CurlEducation - Last Modified:  Aug 27, 2008 8:52 AM by CurlEducation

Transforming XML

The last Curl Cue addressed how to access XML data from a server and store the information as an XML document model (XDMNodes). Now the content must be transformed or processed to meet the application functionality. Since the data has already been internalized, all we need to do is:

  • query the relevant information,
  • perform relevant processesing,
  • and diplay the requested information to the user.

One of the most common types of display is a grid. In the first example, we query data and dislay it using a standard Table and in the second example we will display it using a RecordGrid.

Querying the Data

Once the data is contained within the Curl XML DOM structure, the goal is to extract elements and their associated values by traversing the node tree. This includes element types, element values, and element attributes. Using the WSDK, there are three different ways to obtain this information. Either by using object methods, XPath expressions, or elaboration. In this Curl Cue, we will focus on using XPath expressions to query relevant content.

We will use the same data from the last Curl Cue (XML Data). The following is the XDMDocument view so that you can recall the structure of the XML.

Using XPath to Query Data

All of the nodes in the XDMDocument can be queried using an XPath expression. Nodes include XDMElement,XDMAttribute, XDMText, and XDMProcessingInstruction.

XPath expressions are used to traverse the nodes in the XML structure and extract elements and their associated values. XPath queries return an XPathValue which can be one of the following types:

  • XDMNodeSet
  • String
  • double
  • bool

In our example, a XDMNodeSet is first created to gather all Sample nodes.

def items:XDMNodeSet = {model.search "Samples/Sample"}

Then, standard XPATH expressions are used to query relevant data.

  • item.search "@xx" returns an attribute value of xx
  • item.search "xx" returns an element value of xx

For more information regarding XPath, please see XPath Curl Cue: About XPath.

Tabular Display

In the first example, we will be extracting data from th displaying relevant XML data in a table. This is an example of transforming the data for presentation.

All XPATH queries will return an XDMNodeSet, therefore we need to convert it to a String before it is able to be used in the table. For example:
{item.search "title"}.as-String

Record Display

In the next example, we are using the XPath to query the title, description and link. The information is stored in a RecordSet and displayed to the user with a RecordDisplay. This is an example of transforming the data into a model for further processing. The model data is then transformed for presentation in to a grid display.

For more information about RecordSets, please see RecordSets.

Average User Rating
(0 ratings)




There are no comments on this document