Hi Raj,
See below comments about the above stylesheet:
Code: Select all
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:key name="children" match="menuitem" use="parent"/>
The key defines a map, the key of the map is the value of the parent element and the values associated with the key are menuitem elements that have that value for the parent subelement. For instance if you invoke then the key for the value 1 it will return two menuitem elements, the one with node 2 and the one with node 3.
Code: Select all
<xsl:template match="menu">
<xsl:apply-templates select="menuitem[1]"/>
</xsl:template>
This templates matches on the root element and applies the other templates on the first menuitem (I assumed the first menuitem is the root).
Code: Select all
<xsl:template match="menuitem">
<menuitem>
<xsl:apply-templates mode="copy"/>
<xsl:apply-templates select="key('children', node)"/>
</menuitem>
</xsl:template>
This template matches a menuitem and creates a menuitem element to the output and inside that element places the result of applying the templates that have the mode set to "copy" on the matched menuitem content and the result of applying the templates on the children of this node (we call the mapping passing this node value for the key, see the above explanation at the key definition). This will cause the children menuitem to appear inside the parent menuitem and the templates with mode copy will place only the content of the menuitem that we want to keep inside.
Code: Select all
<xsl:template match="name|attr1|attr2" mode="copy">
<xsl:copy-of select="."/>
</xsl:template>
This template is in the "copy" mode and matches the elements we want to keep inside a menuitem. Matched elements are copied as they are to the output.
Code: Select all
<xsl:template match="*" mode="copy"/>
This template is in the "copy" mode and matched other elements (that we do not want to copy to the output). In this case this will match parent and node for instance. As the template does notheing they will be ignored.
Jeni Tennison's books are very good for understanding XSLT, you can find them together with other very good XSLT books on our books section:
http://www.oxygenxml.com/xml_books.html
Best Regards,
George