Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To start with, if you are using XSLT1.0, you could use a technique known as Muenchian Grouping to get the distinct start time of all programs. This involves setting up a key to look-up programs by their start element.</p> <pre><code>&lt;xsl:key name="hatchala" match="Program" use="Start"/&gt; </code></pre> <p>Note you don't necessarily need the full Xpath to the <strong>Program</strong> elements, as the key will look them up at any level (You would only quantify things if you had such elements at different levels, and wanted to restrict them).</p> <p>Then, to get the distinct start times, you would look up the programs which happen to be the first occurence of that element in the key for their start time</p> <pre><code>&lt;xsl:apply-templates select="/TVGuide/Channel/Program [generate-id() = generate-id(key('hatchala', Start)[1])]" mode="header"&gt; </code></pre> <p>(The use of 'mode' will be clear shortly because the final XSLT will match <strong>Program</strong> elements multiple times).</p> <p>I would also define a key to look up programs for a given channel on a given start time</p> <pre><code>&lt;xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/&gt; </code></pre> <p>Then, for a given start time, you would loop over all channels, like so....</p> <pre><code>&lt;xsl:apply-templates select="/TVGuide/Channel" mode="program"&gt; &lt;xsl:with-param name="Start" select="Start"/&gt; &lt;/xsl:apply-templates&gt; </code></pre> <p>And then template matching this, could look up the program using the key</p> <pre><code>&lt;xsl:template match="Channel" mode="program"&gt; &lt;xsl:param name="Start"/&gt; &lt;td&gt; &lt;xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" mode="program"/&gt; &lt;/td&gt; &lt;/xsl:template&gt; </code></pre> <p>Here is the full XSLT</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="html" indent="yes"/&gt; &lt;xsl:key name="hatchala" match="Program" use="Start"/&gt; &lt;xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/&gt; &lt;xsl:template match="/TVGuide"&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;th&gt;hours&lt;/th&gt; &lt;xsl:apply-templates select="Channel" mode="header"/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt; &lt;xsl:apply-templates select="/TVGuide/Channel/Program[generate-id() = generate-id(key('hatchala', Start)[1])]" mode="header"&gt; &lt;xsl:sort select="Start"/&gt; &lt;/xsl:apply-templates&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Channel" mode="header"&gt; &lt;th&gt; &lt;xsl:value-of select="Name"/&gt; &lt;/th&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Program" mode="header"&gt; &lt;tr&gt; &lt;td&gt; &lt;xsl:value-of select="Start"/&gt; &lt;/td&gt; &lt;xsl:apply-templates select="/TVGuide/Channel" mode="program"&gt; &lt;xsl:with-param name="Start" select="Start"/&gt; &lt;/xsl:apply-templates&gt; &lt;/tr&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Channel" mode="program"&gt; &lt;xsl:param name="Start"/&gt; &lt;td&gt; &lt;xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" mode="program"/&gt; &lt;/td&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Program" mode="program"&gt; &lt;xsl:choose&gt; &lt;xsl:when test="Title!=''"&gt; &lt;xsl:value-of select="Title"/&gt; &lt;/xsl:when&gt; &lt;xsl:otherwise&gt; &lt;xsl:value-of select="Series"/&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Then the following is output</p> <pre><code>&lt;table border="1"&gt; &lt;tr&gt; &lt;th&gt;hours&lt;/th&gt; &lt;th&gt;BBC1&lt;/th&gt; &lt;th&gt;BBC2&lt;/th&gt; &lt;th&gt;ITV&lt;/th&gt; &lt;th&gt;Channel 4&lt;/th&gt; &lt;th&gt;Channel 5&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T19:00:00&lt;/td&gt; &lt;td&gt;A QuestionOfSport&lt;/td&gt; &lt;td&gt;Snooker&lt;/td&gt; &lt;td&gt;Emmerdale&lt;/td&gt; &lt;td&gt;Channel4News&lt;/td&gt; &lt;td&gt;MovieChartShow&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T19:30:00&lt;/td&gt; &lt;td&gt;EastEnders&lt;/td&gt; &lt;td/&gt; &lt;td&gt;CoronationStreet&lt;/td&gt; &lt;td/&gt; &lt;td&gt;FiveNews&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T19:55:00&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;td&gt;SlotArt&lt;/td&gt; &lt;td/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T20:00:00&lt;/td&gt; &lt;td&gt;Get Real with Casualty&lt;/td&gt; &lt;td&gt;HomeFront&lt;/td&gt; &lt;td&gt;Millionaire&lt;/td&gt; &lt;td&gt;Brookside&lt;/td&gt; &lt;td&gt;The World's Worst Drivers Caught On Tape&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T20:30:00&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;td&gt;Brookside&lt;/td&gt; &lt;td/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T20:45:00&lt;/td&gt; &lt;td&gt;Lottery&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T21:00:00&lt;/td&gt; &lt;td/&gt; &lt;td&gt;WildAfrica&lt;/td&gt; &lt;td&gt;Hot Money&lt;/td&gt; &lt;td&gt;Swallow&lt;/td&gt; &lt;td&gt;Black and White&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T21:30:00&lt;/td&gt; &lt;td&gt;Panorama&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T21:50:00&lt;/td&gt; &lt;td/&gt; &lt;td&gt;Nakedness&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;2001-07-05T22:00:00&lt;/td&gt; &lt;td/&gt; &lt;td/&gt; &lt;td/&gt; &lt;td&gt;AllyMcBeal&lt;/td&gt; &lt;td/&gt; &lt;/tr&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre> <p>Do note the use of <strong>xsl:apply-templates</strong> instead of <strong>xsl:for-each</strong> to avoid too much nested code, to try and make it easier to read.</p> <p>If you want a version that mainly uses <strong>xsl:for-each</strong>, try this XSLT instead</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="html" indent="yes"/&gt; &lt;xsl:key name="hatchala" match="Program" use="Start"/&gt; &lt;xsl:key name="hatchala2" match="Program" use="concat(../Name, '|', Start)"/&gt; &lt;xsl:template match="/TVGuide"&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;th&gt;hours&lt;/th&gt; &lt;xsl:for-each select="Channel"&gt; &lt;th&gt; &lt;xsl:value-of select="Name"/&gt; &lt;/th&gt; &lt;/xsl:for-each&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt; &lt;xsl:for-each select="/TVGuide/Channel/Program[generate-id() = generate-id(key('hatchala', Start)[1])]"&gt; &lt;xsl:sort select="Start"/&gt; &lt;xsl:variable name="Start" select="Start" /&gt; &lt;tr&gt; &lt;td&gt; &lt;xsl:value-of select="$Start"/&gt; &lt;/td&gt; &lt;xsl:for-each select="/TVGuide/Channel"&gt; &lt;td&gt; &lt;xsl:apply-templates select="key('hatchala2', concat(Name, '|', $Start))" /&gt; &lt;/td&gt; &lt;/xsl:for-each&gt; &lt;/tr&gt; &lt;/xsl:for-each&gt; &lt;/th&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Program"&gt; &lt;xsl:value-of select="Series"/&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Program[Title!='']"&gt; &lt;xsl:value-of select="Title"/&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>This should generate the same output. Note that I have left one <strong>xsl:apply-templates</strong> in, as it generates the benefits of pattern matching, and provides an alternative to <strong>xsl:choose</strong>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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