I have a problem when parsing dynamic xml data into a html table. The XML is as follow.
<results>
<result id="1" desc="Voltage and current">
<measure desc="VOLT" value="1.0" />
<measure desc="AMPERE" value="2.0" />
</result>
<result id="2" desc="Current-1">
<measure desc="AMPERE" value="5.0" />
</result>
</results>
from which I would like a html table like:
ID DESC VOLT AMPERE
1 Voltage and current 1.0 2.0
2 Current-1 5.0
Notice the empty cell at second voltage column. ID and DESC is taken from result/@id and result/@desc and the rest of the column names should come from measure/@desc
No column name should be duplicate, I managed to code that far, but when I start adding my measures I need to match each measure/@desc to correct column in the table. I tried double nested loops to first match all unique column names, and then loop all measures again to match the column header. But the xslt parser threw a NPE on me!
Sorry that I can’t show any code as it is on a non-connected computer.
I’ve browsed so many Q/A here on SO but to no help for my specific problem.
Thanks in advance
Note: I am able to change the XML format in any way to make parsing easier if anyone come up with a neater format.
If you are using XSLT1.0, you can use a technique called ‘Muenchian’ grouping to get the distinct measure descriptions, which will form the basis of your head row, and also be used to output the values of each row.
Firstly, you define a key to look up measure elements by their @desc attribute
Then, to get the distinct measure descriptions you can iterate over the measure elements that appear first in the group for their given @desc attribute
Then, for your header, you would simply have a template to output the description.
For each result row, would do a similar thing, and iterate over all distinct measure values, but the only difference is you would have to pass in the current result element as a parameter, for later use.
Then, in the template that matched the measure this time, you could access the measure within the result element with a matching @desc attribute (and id there is no such attribute, nothing is output for the cell)
Here is the full XSLT
Note the use of the mode attributes because you have two templates matching the measure element, which function in different ways.
When applied to your input XML, the following is output