How to fix this XSLT to group this XML node to parent that has specific attribute?

kyLox

New Member
I need to merge this node together, but it has to be merged with the parent that has an attribute \[code\]method="a"\[/code\]Input:\[code\]<myroot> <elem name="a" creationDate=""> <list id="xxx" ver="uu"> <nodeA id="a"> <fruit id="small"> <orange id="x" method="create"> <color>Orange</color> </orange> </fruit> <fruit id="small" method="a"> <kiwi id="y" method="create"> <color>Red</color> <type>sour</type> </kiwi> </fruit> <fruit id="large" method="a"> <melon id="y" method="create"> <attributes> <color>Green</color> <type>sweet</type> </attributes> </melon> </fruit> </nodeA> </list> </elem></myroot>\[/code\]my XSL file:\[code\]<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> <xsl:output indent="yes"/> <xsl:strip-space elements="*"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* , node()"/> </xsl:copy> </xsl:template> <xsl:template match="list"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:for-each-group select="/*/*/*/*" group-by="@id"> <xsl:copy> <xsl:copy-of select="@*"/> <xsl:for-each-group select="current-group()/*" group-by="concat(local-name(), '|', @id)"> <xsl:copy> <xsl:apply-templates select="@*, *, (current-group() except .)/*"/> </xsl:copy> </xsl:for-each-group> </xsl:copy> </xsl:for-each-group> </xsl:copy> </xsl:template></xsl:stylesheet>\[/code\]my output:\[code\]<myroot> <elem name="a" creationDate=""> <list id="xxx" ver="uu"> <nodeA id="a"> <fruit id="small"> <orange id="x" method="create"> <color>Orange</color> </orange> <kiwi id="y" method="create"> <color>Red</color> <type>sour</type> </kiwi> </fruit> <fruit id="large" method="a"> <melon id="y" method="create"> <attributes> <color>Green</color> <type>sweet</type> </attributes> </melon> </fruit> </nodeA> </list> </elem></myroot>\[/code\]Expected Output:\[code\]<myroot> <elem name="a" creationDate=""> <list id="xxx" ver="uu"> <nodeA id="a"> <fruit id="small" method="a"> <!-- this is the correct merge where the merged is in parent that has a method --> <kiwi id="y" method="create"> <color>Red</color> <type>sour</type> </kiwi> <orange id="x" method="create"> <color>Orange</color> </orange> </fruit> <fruit id="large" method="a"> <melon id="y" method="create"> <attributes> <color>Green</color> <type>sweet</type> </attributes> </melon> </fruit> </nodeA> </list> </elem></myroot>\[/code\]As we can see my transformation only combine it together and does not consider the method. How to change it so it will be merged to parent that has \[code\]method="a"\[/code\] in the example it is \[code\]<fruit id="small" method="a">\[/code\]Thank you. John
 
Back
Top