Given an input xml file with following structure:
<root>
<record row="1" col="1" val="1" />
<record row="1" col="2" val="2" />
<record row="1" col="3" val="3" />
<record row="1" col="n" val="4" />
<record row="2" col="1" val="5" />
<record row="2" col="3" val="6" />
<record row="2" col="n" val="7" />
<record row="n" col="2" val="8" />
<record row="n" col="3" val="9" />
<record row="n" col="n" val="10" />
</root>
How can I output the following structure using XSLT?
<root>
<row id="1">
<col id="1">1</col>
<col id="2">2</col>
<col id="3">3</col>
<col id="n">4</col>
</row>
<row id="2">
<col id="1">5</col>
<col id="2"></col>
<col id="3">6</col>
<col id="n">7</col>
</row>
<row id="n">
<col id="1"></col>
<col id="2">8</col>
<col id="3">9</col>
<col id="n">10</col>
</row>
</root>
[Note how all columns are output even if there is no related element in input]
EDIT: I may have caused confusion through the use of numbers and letters in my example. The solution I am looking for needs to handle row and column attributes that are non-numeric.
The answers to this question show possible ways to approach the problem:
xslt: How could I use xslt to create a table with multiple columns and rows?
EDIT: A solution that incorporates the techniques seen in the linked question follows.
I am assuming:
@row
and@col
attributes are incrementing numbers that define the position of the record in the table, and they cannot really contain the string"n"
. As such they are not unique throughout the document, which makes them unsuitable as HTML@id
attributes. I substituted them by@title
attributes in my output.@row
continuity will not produce empty rows), only implicit empty cells.@row
and@col
combination is unique.This XSLT 1.0 transformation:
…when applied to this (slightly modified) input:
…produces:
<record>
s of each@row
group<xsl:key>
is used to pinpoint a record by it's position<td>
s, independent of the actual existence of a<record>
at the named positionAn XSLT 2.0 solution
This transformation:
when applied on this XML document:
produces the wanted result: