Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>This solution uses no recursion and hi-lights a few useful XSLT techniques such as Muenchian grouping, keys, finding maximum and iterating without recursion.</strong></p> <p><strong>This transformation</strong>:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output omit-xml-declaration="yes" indent="yes"/&gt; &lt;xsl:strip-space elements="*"/&gt; &lt;xsl:key name="kSectsByValue" match="SectionName" use="."/&gt; &lt;xsl:key name="kDocBySect" match="Document" use="../SectionName"/&gt; &lt;xsl:variable name="vCols" select= "/*/*/SectionName [generate-id() = generate-id(key('kSectsByValue',.)[1]) ]"/&gt; &lt;xsl:variable name="vMaxRows"&gt; &lt;xsl:for-each select="$vCols"&gt; &lt;xsl:sort data-type="number" order="descending" select="count(key('kDocBySect', .))" /&gt; &lt;xsl:if test="position() = 1"&gt; &lt;xsl:value-of select="count(key('kDocBySect', .))"/&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/xsl:variable&gt; &lt;xsl:template match="/"&gt; &lt;table&gt; &lt;tr&gt; &lt;xsl:apply-templates select="$vCols"/&gt; &lt;/tr&gt; &lt;xsl:for-each select= "(/*/*/Document)[not(position() &gt; $vMaxRows)]"&gt; &lt;tr&gt; &lt;xsl:variable name="vPos" select="position()"/&gt; &lt;xsl:for-each select="$vCols"&gt; &lt;td&gt; &lt;xsl:value-of select= "../Document[$vPos]/FileName"/&gt; &lt;/td&gt; &lt;/xsl:for-each&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template match="SectionName"&gt; &lt;td&gt; &lt;xsl:value-of select="." /&gt; &lt;/td&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p><strong>when applied on the original XML document (corrected to be well-formed):</strong></p> <pre><code>&lt;Documents&gt; &lt;Section&gt; &lt;SectionName&gt;Green&lt;/SectionName&gt; &lt;Document&gt; &lt;FileName&gt;Tier 1 Schedules&lt;/FileName&gt; &lt;/Document&gt; &lt;Document&gt; &lt;FileName&gt;Tier 3 Schedules&lt;/FileName&gt; &lt;/Document&gt; &lt;Document&gt; &lt;FileName&gt;Setback Schedule&lt;/FileName&gt; &lt;/Document&gt; &lt;Document&gt; &lt;FileName&gt;Tier 2 Governance&lt;/FileName&gt; &lt;/Document&gt; &lt;/Section&gt; &lt;Section&gt; &lt;SectionName&gt;MRO/Refurb&lt;/SectionName&gt; &lt;Document&gt; &lt;FileName&gt;Tier 2 Governance&lt;/FileName&gt; &lt;/Document&gt; &lt;/Section&gt; &lt;/Documents&gt; </code></pre> <p><strong>produces the desired result</strong>:</p> <pre><code>&lt;table&gt; &lt;tr&gt; &lt;td&gt;Green&lt;/td&gt; &lt;td&gt;MRO/Refurb&lt;/td&gt; &lt;/tr&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;/tr&gt; &lt;tr&gt; &lt;td&gt;Setback Schedule&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;/tr&gt; &lt;/table&gt; </code></pre> <p><strong>Do note</strong>:</p> <ol> <li><p><strong>We use the <a href="http://en.wikipedia.org/wiki/Muenchian_grouping" rel="nofollow noreferrer">Muenchian method for grouping</a></strong> in order to find all different column names, not relying that in the XML document they will be unique.</p></li> <li><p><strong><a href="http://www.w3.org/TR/xslt#key" rel="nofollow noreferrer">Keys</a> are used</strong> both for the Muenchian grouping and for finding all items belonging to a column.</p></li> <li><p><strong>The maximum number of rows is found</strong> and kept in the variable <code>$vMaxRows</code></p></li> <li><p><strong>We iterate N times</strong> to produce the N rows of the table -- <strong>not using recursion</strong>!</p></li> <li><p><strong>The <code>N</code>-th row is output by applying templates to all column items</strong> that have position <code>N</code> in their column.</p></li> </ol>
 

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