XPath data source is used to import data from an external XML document. The XML document location is given with a source attribute. Attributes value can be a XML URL or a name of a object whose toString() method returns the XML content. This object should be available in a JSP request context, if Chart XML is used from a Chart tag library.
The relevant parts of the XML document are selected using an XPath expression. The resulting set of nodes is mapped to data tuples. Nodes can be mapped to data tuples automatically, or the mapping can be specified with using <xpathColumn> tags.
This data source requires Apache Xalan XSL-T engine to be placed in the class path. If you are using SUN JDK 1.4.x the XSL-T engine is allready included.
For example, assuming an XML document
<employees> <employee> <name>Anne</name> <salary>1200</salary> </employee> <employee> <name>Bill</name> <salary>1150</salary> </employee> <employee> <name>Carol</name> <salary>1300</salary> </employee> <employee> <name>David</name> <salary>1400</salary> </employee> </employees>
and XPath "//employees/employee", the resulting node set contains the four <employee> nodes.
Each of these nodes is mapped to two data columns. A straight forward mapping is achieved by defining two <xpathColumn> tags:
<data> <xpath source="employees.xml" select="//employees/employee"> <xpathColumn column="name" type="label" /> <xpathColumn column="salary" type="value" /> </xpath> </data>
This results in the following data stream:
1:('Anne',1200)
2:('Bill',1150)
3:('Carol',1300)
4:('David',1400)
which can be visualized by a chart.
|
| Employees.xml visualized using a chart. |
If there are no <xpathColumn> tags defined an automatic column detection is carried out. Automatic column detection searches for children nodes that appear in all nodes in xpath result set. If all the nodes contain children with the same name, the children in question is detected as a data column. If the first text child can be converted to a double, the type of the output column is set to value. Otherwise, the type of the output column is set to label.
Assume that we have the following xml document:
<zoo>
<animal>
<eyes>2</eyes>
<legs>4</legs>
<race>zebra</race>
<stripes>12</stripes>
</animal>
<animal>
<eyes>8</eyes>
<legs>8</legs>
<race>redback</race>
</animal>
<animal>
<eyes>2</eyes>
<legs>2</legs>
<race>duck</race>
<wings>2</wings>
</animal>
</zoo>
with select string is "//zoo/animal". This results in three animal nodes. Three different columns would be identified: 'eyes', 'legs' and 'race'. The 'eyes' and 'legs' columns would be identified to be of type value, because their first text element can be converted to a number. The 'race' column would be assigned to be of type label, because standard number conversion fails to transform 'zebra' to a number.
The resulting data stream would be similar to:
1: (2,4,'zebra') 2: (8,8,'redback') 3: (2,2,'duck')
Thus given the above xml and configuration:
<data> <xpath source="zoo.xml" select="//zoo/animal"/> </data>
|
| Automatic column detection. |