I have a grouping issue where i need to group all the keys that has the same number under a node. For e.g., my XML looks like :
<results>
<status>completed</status>
<info>success</info>
<prod1>abc</prod1>
<pub1>test</pub1>
<sub1>123</sub1>
<subtype1>pt</subtype1>
<prod2>def</prod2>
<pub2>test22</pub2>
<sub2>456</sub2>
<subtype2>pt</subtype2>
<prod3>ghi</prod3>
<pub3>test33</pub3>
<sub3>789</sub3>
<subtype3>pt</subtype3>
</results>
I need to convert the above into:
<results>
<status>completed</status>
<info>success</info>
<products>
<product>
<prod>abc</prod>
<pub>test</pub>
<sub>123</sub>
<subtype>pt</subtype>
</product>
<product>
<prod>def</prod>
<pub>test22</pub>
<sub>456</sub>
<subtype>pt</subtype>
</product>
<product>
<prod>ghi</prod>
<pub>test33</pub>
<sub>789</sub>
<subtype>pt</subtype>
</product>
</products>
</results>
Any help in resolving the above is highly appreciated. I am currently stuck with this issue and not able to proceed.
The below xslt pulls each element and puts into a node and i m not able to group all elements that ends with a particular number into a single node.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="elementByRow" match="/*/*"
use="(name(.)[1])" />
<xsl:template match="/messages">
<messages>
<!-- pick out the first RowN.* element for each N -->
<xsl:apply-templates select="*[generate-id() =
generate-id(key('elementByRow', name(.))[1])]" />
</messages>
</xsl:template>
<xsl:template match="*">
<row>
<!-- process _all_ the elements that belong to this row -->
<xsl:for-each select="key('elementByRow', name(.))[1]">
<xsl:element name="{name(.)[1]}">
<xsl:value-of select="." />
</xsl:element>
</xsl:for-each>
</row>
</xsl:template>
</xsl:stylesheet>