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.
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.
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.
The expression is then visible when the cursor rolls over the condition:
We will also use the XPath expression in the Chart Settings dialog to select rows of data to be charted:
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.
We can insert the combo box at the top of the form, above the description and chart.
StyleVision offers several ways to define combo box behavior. We will use a list of visible entries with corresponding 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.
The new XPath expression for the chart data is $XML/data/set[@setnum=$XML2/userchoice/chart]/row
shown below 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:
After we resize the combo box to fit the widest entry, the e-Form looks like this:
The combo box allows the user to select any other chart:
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:
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!