Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is one possible solution:</p> <pre><code>&lt;xsl:variable name="vCountRows"&gt; &lt;xsl:apply-templates select="Documents/Section[1]" mode="findmax" /&gt; &lt;/xsl:variable&gt; &lt;xsl:variable name="vCountCols" select="count(Documents/Section)" /&gt; &lt;xsl:template match="/Documents"&gt; &lt;table r="{$vCountRows}" s="{$vCountCols}"&gt; &lt;thead&gt; &lt;xsl:call-template name="create-thead" /&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;xsl:call-template name="create-tr" /&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template name="create-thead"&gt; &lt;tr&gt; &lt;xsl:apply-templates select="Section" /&gt; &lt;/tr&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Section"&gt; &lt;th&gt;&lt;xsl:value-of select="SectionName" /&gt;&lt;/th&gt; &lt;/xsl:template&gt; &lt;xsl:template name="create-tr"&gt; &lt;xsl:param name="row" select="1" /&gt; &lt;tr&gt; &lt;xsl:call-template name="create-td"&gt; &lt;xsl:with-param name="row" select="$row" /&gt; &lt;/xsl:call-template&gt; &lt;/tr&gt; &lt;xsl:if test="$row &amp;lt; $vCountRows"&gt; &lt;xsl:call-template name="create-tr"&gt; &lt;xsl:with-param name="row" select="$row + 1" /&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template name="create-td"&gt; &lt;xsl:param name="col" select="1" /&gt; &lt;xsl:param name="row" select="1" /&gt; &lt;td&gt; &lt;xsl:value-of select="Section[$col]/Document[$row]/FileName" /&gt; &lt;/td&gt; &lt;xsl:if test="$col &amp;lt; $vCountCols"&gt; &lt;xsl:call-template name="create-td"&gt; &lt;xsl:with-param name="col" select="$col + 1" /&gt; &lt;xsl:with-param name="row" select="$row" /&gt; &lt;/xsl:call-template&gt; &lt;/xsl:if&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Section" mode="findmax"&gt; &lt;xsl:variable name="c" select="count(Document)" /&gt; &lt;xsl:variable name="next" select="following-sibling::Section[count(Document) &amp;gt; $c][1]" /&gt; &lt;xsl:choose&gt; &lt;xsl:when test="$next"&gt; &lt;xsl:apply-templates select="$next" mode="findmax" /&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="$c" /&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; </code></pre> <p>With your input it produces:</p> <pre><code>&lt;table&gt; &lt;thead&gt; &lt;tr&gt; &lt;td&gt;Green&lt;/td&gt; &lt;td&gt;MRO/Refurb&lt;/td&gt; &lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td&gt;Tier 1 Schedules&lt;/td&gt; &lt;td&gt;Tier 2 Governance&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Tier 3 Schedules&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Setback Schedule&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Tier 2 Governance&lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>The general apporach goes like this:</p> <ol> <li>Find out how many rows we are going to get (this happens in <code>&lt;xsl:template match="Section" mode="findmax"&gt;</code>, which recursively finds the section with the maximum number of <code>&lt;Document&gt;</code> nodes</li> <li>Find out how many columns we are going to get (by counting the number of <code>&lt;Section&gt;</code>s)</li> <li>calling a templates that creates a <code>&lt;tr&gt;</code>, and keeps calling itself until all necessary rows have been created</li> <li>in this template, a second template is called, this one creates the <code>&lt;td&gt;</code>s. It keeps calling itself until it reaches the max number of columns (from step 2)</li> </ol> <p>The algorithm:</p> <ul> <li>uses incrementing column- and row-numbers as indexes</li> <li>uses recursion to achieve one-by-one index increments (since actually incrementing a variable is impossible in XSLT)</li> <li>creates the right number of empty cells for sections that have less documents</li> </ul> <p>A more efficient version (probably using <code>&lt;xsl:key&gt;</code>s) exists, I'll look into optimizing mine a little more.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload