XPath Expressions for Data Reporting


In our earlier post titled Use XPath Expressions to Refine Data Selection, we described how to use XMLSpy to develop an XPath expression to select one table of data contained in a much larger data set provided by the US Department of Education.

An HTML report based on XPath data selection in StyleVision

We can reuse the work in XMLSpy to quickly create a StyleVision design for a report or an e-Form to communicate highlights from the data.

We can start with a simple design in StyleVision based on the gradData XML Schema and working XML file we created in XMLSpy. Since we saved our final refined chart settings in XMLSpy, we can also reload the chart design in StyleVision.

StyleVision design for the data report

We’ll use the XPath expression /data/set[@setnum=”FOS”] developed in XMLSpy in two places to select only one of the tables contained in the larger XML file. This expression will only match the /data/set whose setnum attribute is FOS: <set setnum=”FOS”>.

First, we will use the expression to define conditional output of the <description> element. Each individual table includes a <description> that will make a useful chart title. We can enter the XPath expression in the Properties helper window for the condition.

Properties window for the condition control, showing the XPath expression

The expression is then visible when the cursor rolls over the condition:

XPath displayed in the design window

We will also use the XPath expression in the Chart Settings dialog to select rows of data to be charted:

XPath for data selection in the chart settings dialog

These two instances generated the HTML output shown in the first image at the top of the post.

An XPath Expression Plus a Combo Box Equals User Choice

We can convert the report to an interactive Authentic e-Form by using an XML element as a variable in the XPath expression instead of the hard-coded string value “FOS”. We can then allow the user to set the content of the XML element via a combo box.

To implement this functionality, we will insert a second working XML file to store the temporary user selection, shown below in the StyleVision Design Overview and Schema Tree windows.

Design Overview and Schema Tree showing multiple working XML files

We can insert the combo box at the top of the form, above the description and chart.

A combo box added to the design for interactive control

StyleVision offers several ways to define combo box behavior. We will use a list of visible entries with corresponding XML values.

Combo box defintion of Visible Entries and XML values

The combo box will assign an XML value to <chart> in the userselections.xml file, which we can then match against the setnum attribute in the gradData.xml file.

Next, we need to modify both XPath expressions. The new XPath expression for the condition assigned to <description> is

../@setnum=$XML2/userchoice/chart

as shown below in the Properties window.

Updated XPath expression for user-selected display

The new XPath expression for the chart data is $XML/data/set[@setnum=$XML2/userchoice/chart]/row

shown below in the chart settings dialog.

Updated XPath in the chart settings dialog

StyleVision includes an XPath Expression editor you can access by clicking the edit button next to either XPath expression shown above. The XPath Expression editor has a Builder mode and an Evaluator mode to assist writing XPath 1.0 and XPath 2.0. The Builder mode provides on-the-fly syntax checking and an auto-completion pop-up for efficiency. The XPath Editor is shown in reduced size below:

XPath Builder (reduced view)

After we resize the combo box to fit the widest entry, the e-Form looks like this:

Interactive e-Pub for data reporting

The combo box allows the user to select any other chart:

New user selection from combo-box drop-down

The XML value of the new selection replaces the contents of the <chart> element in the userselections.xml file, and both XPath expressions generate new results:

Updated e-Pub report

Click here to download a free trial of the Altova MissionKit, including XMLSpy, MapForce, and StyleVision and get started with XPath to refine your own data selections!

Tags: , , , , ,