How would I take this xml and create a table with a column for each "section" element and then display all the "document" elements in that column using xslt?
<Documents>
<Section>
<SectionName>Green</SectionName>
<Document>
<FileName>Tier 1 Schedules</FileName>
</Document>
<Document>
<FileName>Tier 3 Schedules</FileName>
</Document>
<Document>
<FileName>Setback Schedule</FileName>
</Document>
<Document>
<FileName>Tier 2 Governance</FileName>
</Document>
</Section>
<Section>
<SectionName>MRO/Refurb</SectionName>
<Document>
<FileName>Tier 2 Governance</FileName>
</Document>
</Section>
Thanks, Al
This solution uses no recursion and hi-lights a few useful XSLT techniques such as Muenchian grouping, keys, finding maximum and iterating without recursion.
This transformation:
when applied on the original XML document (corrected to be well-formed):
produces the desired result:
Do note:
We use the Muenchian method for grouping in order to find all different column names, not relying that in the XML document they will be unique.
Keys are used both for the Muenchian grouping and for finding all items belonging to a column.
The maximum number of rows is found and kept in the variable
$vMaxRows
We iterate N times to produce the N rows of the table -- not using recursion!
The
N
-th row is output by applying templates to all column items that have positionN
in their column.This is one possible solution:
With your input it produces:
The general apporach goes like this:
<xsl:template match="Section" mode="findmax">
, which recursively finds the section with the maximum number of<Document>
nodes<Section>
s)<tr>
, and keeps calling itself until all necessary rows have been created<td>
s. It keeps calling itself until it reaches the max number of columns (from step 2)The algorithm:
A more efficient version (probably using
<xsl:key>
s) exists, I'll look into optimizing mine a little more.