Given the following XML:
<results name="queryResults">
<int name="intfield1:[* TO 10]">11</int>
<int name="intfield2:[10 TO 20]">9</int>
<int name="intfield1:[10 TO 20]">12</int>
</results>
I would like to produce this XML:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
<value name="[10 TO 10]">12</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
</results>
I can't think how to do this in XSL mainly because i'm wanting to Group by the numbericfield.. All i can come up with is this:
<xsl:if test="count(results/int) > 0">
<results>
<xsl:for-each select="results/int">
<field>
<xsl:attribute name="name">
<xsl:value-of select="substring-before(@name, ':')"/></xsl:attribute>
<value>
<xsl:attribute name="name">
<xsl:value-of select="substring-after(@name, ':') "/>
</xsl:attribute>
<xsl:value-of select="."/>
</value>
</field>
</xsl:for-each>
</results>
</xsl:if>
However this doesn't produce the nice grouped list instead i get this:
<results>
<field name="numberfield1">
<value name="[* TO 10]">11</value>
</field>
<field name="numberfield2">
<value name="[10 TO 20]">9</value>
</field>
<field name="numberfield1">
<value name="[10 TO 10]">12</value>
</field>
</results>
If someone can stear me in the right direction.. That would be great?
Thanks
To do this in XSLT 1.0, you will have to use a technique called "muenchian grouping". First create a key of the nodes on which you wish to group
Next, you iterate it through all the nodes, but only selecting the ones that happen to be first in the relevant group
Next, you can iterate use the key to iterate over all nodes in the group
Putting this all together gives
In your example, 'intfield' becomes 'numberfield' though. I have kept the name as 'intfield' in the above example.
Muenchian grouping is a work of genius. It's not easy to understand, but see: http://www.jenitennison.com/xslt/grouping/muenchian.html
To simplify the process the W3C specifically supported grouping in XSLT2.0. See, for example: http://www.xml.com/pub/a/2003/11/05/tr.html
However not all environments support XSLT2.0