This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Shan, Here is how the template stuff works in XSL - There is a default template defined in XSL as given below - <!-- Default tempalte for root or any element --> <xsl:template match="/|*"> <xsl:apply-templates /> </xsl:template> The above template will take care of recursing the tree of nodes "until it finds a matching template rule". "/" -----Source | --------title | |________"XSL" --------author | |________"John Smith" --------auth | |________"John Smith" --------author |________"John Smith" The default template for text() nodes is - <xsl:template match="text()|@*"> <xsl:value-of select="."/> </xsl:template> Hence when no template match rule is explicitly mentioned by us for a node, the default template will take control. <xsl:apply-templates /> will propogate the recursion down the tree. Now try to apply this concept to the above node tree and things will get clear to you.
Shan, I think if you get the point of differentiating b/w "/" and "//", things will be a piece of cake for you. Here we go, Here is some terminology to consider - "/" ---> This is the document root when used at the begining as in "/source"; this looks for the root element child(source in this case)right under the document root; again this depends on the context under which it has been used; please look at the example below - /source/AAA --> the second "/" in this xpath expression before AAA says that get all the children (only first level children) under the source. Little example - <source> <AAA/> <AAA/> <BBB> <AAA/> </BBB> </source> /source/AAA gets only the first two AAA (not the one inside BBB), while /source//AAA gets all the AAA children under source irrespective of the level of nesting. Now with this footing, let's look at the real thing - I got this stuff pasted from the actual example stylesheet in the link provided - <xsl:apply-templates select="/source//AAA/BBB//*"/> Please look at the xml below (copied from the link u provided) - ============================================== <source> <AAA id="a1" pos="start"> <BBB id="b1"/> <BBB id="b2"/> </AAA> <AAA id="a2"> <BBB id="b3"/> <BBB id="b4"/> <CCC id="c1"> <DDD id="d1"/> </CCC> <BBB id="b5"> <CCC id="c2"/> </BBB> </AAA> </source> ================================================ This means that get all the AAA descendants (not merely the immediate children)of the source (root element), then for each AAA in that list, get the immediate BBB children (not all the descendants) and get all the children (*) for these BBB elements. The only node that matches this xpath expression is the CCC shown below in its context - <BBB id="b5"> <CCC id="c2"/> </BBB> Hope that this is clear. Please try to play around with the XML document by nesting more elements within each other and try changing the xpath expression and see the results by applying the stylesheet. This will make things much clearer.