I have the following XML code:
<root>
<options>
<companies>
<company url="http://www.brown.com">Brown LLC</company>
<company url="http://www.yellow.com">Yellow LLC</company>
<company url="http://www.black.com">Black LLC</company>
<company url="http://www.bourdeaux.com">Bourdeaux LLC</company>
<company url="http://www.orange.com">Orange LLC</company>
</companies>
</options>
</root>
and I need to do two things with it:
Build a html dropdown with the unique first letters found in the company nodes. Such as:
<select id="colors"> <option value="B">B</option> <option value="O">O</option> <option value="Y">Y</option> </select>
Build a secondary dropdown, which lists all the companies starting with a specific letter. Such as:
<select id="companiesB"> <option value="http://www.black.com">Black LLC</option> <option value="http://www.bordeaux.com">Bordeaux LLC</option> <option value="http://www.brown.com">Brown LLC</option> </select>
Any help would be appreciated!
You should take a look into the Muenchian Method for grouping in XSLT.
Here are two stylseheets, the first for producing the first select:
the second for producing the second select element:
First you need to define a key to 'group' all company elements together that share the same first letter
Next, you would iterate over all company elements
However, you only want to process a company 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
Putting this altogether gives
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.
To call the template, it is simply a case of passing the required parameter, for example
Of course, you could put this is an for-each loop if you wanted to show all drop-downs for all possible first-letters.