I know I can read a file into a variable like this:
<xsl:variable name=”content” select=”document(‘file.xml’)”/>, but how can I do the following:
file.xml:
<root> <child>Some</child> <child>Random</child> <child>Data</child> <child>Stuff</child> </root>
Using that xml file, I’d like to print:
D * Data R * Random S * Stuff * Some
Meaning printing the caption only once and then listing the items alphabetically. I know I can use <xsl:for-each> and <xsl:sort>, but how to do that caption part?
Thanks for any help!
There are several possible solutions, depending on what you want to do. The most well-known is the Muenchen method. This method first defines a lookup table to find all elements that share a particular key by that key. Then, you iterate over all keys (in some order) and for each key, over all key-values (in some order).
<xsl:key>can be used to make lookup tables. If you index of the first letter, you can then retrieve those elements which correspond to a particular letter.Now, we can’t iterate over all keys in a straightforward manner. However, we can iterate over all values and test whether they’re the first value in the lookup table…
The above code will execute something only on child tags where the generate-id function returns the same value as the generate-id function does for the first element of the list of children with the same first letter. Since generate-id generates unique id’s, this will only return only those children that are “first” in the lookup table for their particular key.
Now, you’re almost done: just iterate over all children with that particular first letter:
This is called the Muenchian method.