- You have some node list which contains many, many items.
- You want to group items based on part of their content
- You want to do it in xsl
<phrases> <node type="Greeting">Hello</node> <node type="Farewell">Goodbye</node> <node type="Greeting">Hi</node> <node type="Greeting">Howdy</node> <node type="Farewell">See you later</node> </phrases>
You can use the
This function is part of xsl so you don't need any funky includes, but it means you can't use it from XPath (e.g. from C#'s SelectNodes() method)
<xsl:if test="generate-id(.) = generate-id(../node[@type=current()/@type])> <xsl:value-of select="."/> </xsl:if>
And that's it!
Let's break it down
Does exactly what you might expect i.e. it generates a unique ID which represents the current node. Calling generate-id on the same node twice will return the same id.
This bit gets all the other nodes which have the same value based on the key we are grouping by (in this case