Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This can be solved pretty easily using XPath. Here's the expression you're looking for: <code>count((.|preceding-sibling::ROLE)[not(@name = preceding-sibling::ROLE/@name)])</code></p> <p>This can be broken down to make it more readable, as I've done in the following XSLT 1.0 stylesheet:</p> <pre><code>&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt; &lt;xsl:output method="text"/&gt; &lt;!-- don't copy whitespace --&gt; &lt;xsl:template match="text()"/&gt; &lt;xsl:template match="ROLE"&gt; &lt;xsl:variable name="roles-so-far" select=". | preceding-sibling::ROLE"/&gt; &lt;!-- Only select the first instance of each ROLE name --&gt; &lt;xsl:variable name="roles-so-far-unique" select="$roles-so-far[not(@name = preceding-sibling::ROLE/@name)]"/&gt; &lt;xsl:apply-templates select="@name"/&gt; &lt;xsl:text&gt; &lt;/xsl:text&gt; &lt;xsl:value-of select="count($roles-so-far-unique)"/&gt; &lt;xsl:text&gt;&amp;#xA;&lt;/xsl:text&gt; &lt;!-- linefeed --&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>Here's an alternative implementation, using the Muenchian method. First, declare a key:</p> <pre><code>&lt;xsl:key name="roles" match="ROLE" use="@name"/&gt; </code></pre> <p>Then, replace the definition of $roles-so-far-unique with something like this:</p> <pre><code>&lt;!-- Among all the ROLEs having one of the names so far, select only the first one for each name --&gt; &lt;xsl:variable name="roles-so-far-unique" select="../ROLE[@name = $roles-so-far/@name] [generate-id(.) = generate-id(key('roles',@name)[1])]"/&gt; </code></pre> <p>This code, of course, is more complicated. Unless you have a large data set requiring you to speed up processing using the Muenchian method (even then I would test to make sure it buys you anything), you might as well stick with the simpler version above.</p> <p>Finally, in XSLT 2.0, it's much easier. Simple replace the $roles-so-far-unique definition with the following:</p> <pre><code>&lt;!-- Return a list of distinct string values, with duplicates removed --&gt; &lt;xsl:variable name="roles-so-far-unique" select="distinct-values($roles-so-far/@name)"/&gt; </code></pre> <p>I hope this has helped you identify where you went wrong in the various attempts that you mentioned.</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