Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Another slightly different approach (although not necessarily a better approach) is to pass a parameter to each <strong>student</strong> match which contains a comma-delimited of <strong>group</strong> attributes that have already been output. Every time you match a <strong>student</strong>, you check whether their group is in his parameter, and if not output the student, and get the next one, appending the current group to the parameter.</p> <p>Here is the XSLT, which I have commented to try and explain things better</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output indent="yes"/&gt; &lt;xsl:template match="Socrates"&gt; &lt;Socrates&gt; &lt;xsl:apply-templates select="Student[1]"/&gt; &lt;/Socrates&gt; &lt;/xsl:template&gt; &lt;xsl:template match="Student"&gt; &lt;!-- Parameter containin comma-delimited list of currently output groups --&gt; &lt;xsl:param name="groupList" select="','" /&gt; &lt;xsl:choose&gt; &lt;!-- Has the group already been output? --&gt; &lt;xsl:when test="contains($groupList, concat(',', @group, ','))"&gt; &lt;!-- If so, move on to next student record --&gt; &lt;xsl:apply-templates select="following-sibling::Student[1]"&gt; &lt;xsl:with-param name="groupList" select="$groupList" /&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:when&gt; &lt;!-- Group has not already been output --&gt; &lt;xsl:otherwise&gt; &lt;!-- Output the record --&gt; &lt;xsl:copy-of select="." /&gt; &lt;!-- Get the next student with a different name --&gt; &lt;xsl:apply-templates select="following-sibling::Student[@name!=current()/@name][1]"&gt; &lt;xsl:with-param name="groupList" select="concat($groupList, @group, ',')" /&gt; &lt;/xsl:apply-templates&gt; &lt;/xsl:otherwise&gt; &lt;/xsl:choose&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>When applied to your sample XML, the following is output</p> <pre><code>&lt;Socrates&gt; &lt;Student name="Aristotle" group="1" /&gt; &lt;Student name="Plato" group="2" /&gt; &lt;Student name="Xenophon" group="4" /&gt; &lt;Student name="Critias" group="3" /&gt; &lt;/Socrates&gt; </code></pre> <p>Note that this does assume the <strong>student</strong> elements are always ordered by name in the input XML.</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