Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First you need to define a key to 'group' all <strong>company</strong> elements together that share the same first letter</p> <pre><code>&lt;xsl:key name="companyLetter" match="company" use="substring(text(), 1, 1)" /&gt; </code></pre> <p>Next, you would iterate over all <strong>company</strong> elements</p> <pre><code>&lt;xsl:for-each select="options/companies/company"&gt; </code></pre> <p>However, you only want to process a <strong>company</strong> element if it is the first occurence of that element for its first letter. You do this by looking up the first element in your key for the first letter, and seeing if it is the same. Element comparison is done using the generate-id() function</p> <pre><code>&lt;xsl:variable name="firstLetter" select="substring(text(), 1, 1)" /&gt; &lt;xsl:if test="generate-id(.) = generate-id(key('companyLetter', $firstLetter)[1])" &gt; </code></pre> <p>Putting this altogether gives</p> <pre><code>&lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt; &lt;xsl:key name="companyLetter" match="company" use="substring(text(), 1, 1)"/&gt; &lt;xsl:template match="/root"&gt; &lt;select id="colors"&gt; &lt;xsl:for-each select="options/companies/company"&gt; &lt;xsl:sort select="text()"/&gt; &lt;xsl:variable name="firstLetter" select="substring(text(), 1, 1)"/&gt; &lt;xsl:if test="generate-id(.) = generate-id(key('companyLetter', $firstLetter)[1])"&gt; &lt;option&gt; &lt;xsl:attribute name="value"&gt; &lt;xsl:value-of select="$firstLetter"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:value-of select="$firstLetter"/&gt; &lt;/option&gt; &lt;/xsl:if&gt; &lt;/xsl:for-each&gt; &lt;/select&gt; &lt;/xsl:template&gt; &lt;/xsl:stylesheet&gt; </code></pre> <p>For the second drop-down, you can use a named template that gets passed a letter as a parameter. You can look up all elements for that letter using the same key as above.</p> <pre><code>&lt;xsl:template name="Companies"&gt; &lt;xsl:param name="firstLetter"/&gt; &lt;select&gt; &lt;xsl:attribute name="id"&gt; &lt;xsl:value-of select="$firstLetter"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:for-each select="key('companyLetter', $firstLetter)"&gt; &lt;xsl:sort select="text()"/&gt; &lt;option&gt; &lt;xsl:attribute name="value"&gt; &lt;xsl:value-of select="@url"/&gt; &lt;/xsl:attribute&gt; &lt;xsl:value-of select="text()"/&gt; &lt;/option&gt; &lt;/xsl:for-each&gt; &lt;/select&gt; &lt;/xsl:template&gt; </code></pre> <p>To call the template, it is simply a case of passing the required parameter, for example</p> <pre><code>&lt;xsl:call-template name="Companies"&gt; &lt;xsl:with-param name="firstLetter"&gt;B&lt;/xsl:with-param&gt; &lt;/xsl:call-template&gt; </code></pre> <p>Of course, you could put this is an for-each loop if you wanted to show all drop-downs for all possible first-letters.</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