To get beyond the root, you have to tell the formatting engine to process the children of the root. In general, to include content in the child nodes, you have to recursively process the nodes through the XML document. The element that does this is xsl:apply-templates. By including xsl:apply-templates in the output template, you tell the formatter to compare each child element of the matched source element against the templates in the style sheet, and, if a match is found, output the template for the matched node. The template for the matched node may itself contain xsl:apply-templates elements to search for matches for its children. When the formatting engine processes a node, the node is treated as a complete tree. This is the advantage of the tree structure. Each part can be treated the same way as the whole.
Thank you for the compliment. I learnt a lot of things only after entering this forum. Previously i used to live up with lots of doubts in my mind. I still have to learn a lot of things as i'm not very clear in some of the concepts. I'll try to explain things to the best of my knolwedge. Please look at the below xml file and the following xsl stylesheet (stylesheet has the comments which explain what its exactly doing).
Just a little brush up - The xsl:apply-templates basically has a "select=" stuff which selects a nodeset from the xml tree of nodes to which templates will be applied. You need to have a template matching rule defined for the nodes in the selected list(nodeset) for specific instructions to be carried out in the process of transforming those nodes.
You see that the template has been defined for the "chapter" node here. You can define the course of action inside the
<chapter attr="content"> Content </chapter>
<chapter attr="end">THE END</chapter>
XSLT document .............
<!-- This selects all the chapter elements in the xml document -->
<!-- This template mentions what to do for each chapter elemnet -->
<!-- You can take the necessary action here -->
<!-- Here we are printing the value of the node i.e., the text value
<!-- We can as well get the attribute values as shown below -->
<!-- <xsl:value-of select="@attr"/> -->
Please run the stylesheet on the xml file to see the results. You can uncomment the attribute stuff in the template code and see the results.
Hope this helps.
I feel xsl:apply templates does some thing like xsl:for-each. Am I correct?
<!-- call template matching rule for node -->
<!-- print the nodename -->
will go through each element in the node list and apply the template matching rule. Hence <xsl:for-each> can simulate <xsl:apply-templates> and much more. Hope that i'm clear on this part.
Coming to the xslt processor stuff, i'm using the java processor classes from the Apache's xalan xslt website here -
You will be applying the stylesheets to the xml files from the command line.
The following are the sequence of steps i followed to get the stuff working on my m/c -
1) download xalan-j_2_4_0-bin.zip from the above site
2) extract the zip file in any of the directories
3) You then need to go to the xalan/bin directory and extract the contents of some of the jar files here as "jar -xvf xalan.jar", then the same thing for xercesImpl.jar and xml-apis.jar. This process will basically extract all the class files which will be used by the our xslt processor.
4) The most important thing then is to update the CLASSPATH variable on our m/c with the absolute path of our xalan/bin directory.
5) To ensure that everything is properly set up on ur m/c, you need to run the following command from the command line -
and it should give you a list of options as shown below -
-V (Version info)
-QC (Quiet Pattern Conflicts Warnings)
-TT (Trace the templates as they are being called)
-TG (Trace each result tree generation event)
-TS (Trace each selection event)
-TTC (Trace the template children as they are being processed)
-EDUMP [optional]FileName (Do stackdump on error)
-XML (Use XML formatter and add XML header)
-TEXT (Use simple Text formatter)
-HTML (Use HTML formatter)
-PARAM name value (Set a stylesheet parameter)
-DIAG (Print number of milliseconds transform operation took)
-FLAVOR flavorName (Transform with SAX for s2s or DOM for d2d)
-URIRESOLVER fullClassName (Use a custom URIResolver)
-ENTITYRESOLVER fullClassName (Use a custom EntityResolver)
-CONTENTHANDLER fullClassName (Use a custom ContentHandler)
-INCREMENTAL (Request incremental transform by setting
http://xml.apache.org/xalan/features/incremental to true)
-NOOPTIMIZE (Request no optizimation of stylesheet processing
by setting http://xml.apache.org/xalan/features/optimize to
-RL recursionLimit (Set numeric limit on depht of stylesheet
-L (Turn on source_location attribute)
This make sure that you are all set to go. If you didnot set the classpath stuff properly, you will get the no class defintion found error.
6) Next thing is that you need to write a sample test.xml file (say) and write some xsl file for that (test.xsl) and run the command as follows -
java org.apache.xalan.xslt.Process -in test.xml -xsl test.xsl -out test.txt
You can find all these details at the website mentioned above. Please come back if you face any problems doing this and please correct me for any mistakes.
It teaches the concepts by examples.