• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

xsl:apply-templates

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does the above mean when given in a xsl sylesheet.
I also want to add that Mr Jayadev's post are extremely clear and he explains very well. I appreciate his posts.
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From http://www.ibiblio.org/xml/books/bible2/chapters/ch17.html

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.


Cheers,
Dan
 
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shan,
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
<xsl:template match="chapter">
==============================================
XML document..................
<source>
<chapter attr="intro">Introduction</chapter>
<chapter attr="content"> Content </chapter>
<chapter attr="end">THE END</chapter>
</source>
============================================
XSLT document .............
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<!-- This selects all the chapter elements in the xml document -->
<xsl:apply-templates select="//chapter"/>
</xsl:template>

<!-- This template mentions what to do for each chapter elemnet -->
<xsl:template match="chapter">
<!-- 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"/> -->
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
================================================
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank u jaydev. I have downloaded SPY but the IDE is totally confusing..can u suggest something simple to test.
I feel xsl:apply templates does some thing like xsl:for-each. Am I correct?
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think they are different. xsl:for-each will loop on the nodes in the list "select"ed. Any course of action for the list of nodes can be mentioned within the scope of xsl:for-each, as shown below -
<xsl:for-each select="//chapter">
<!-- call template matching rule for node -->
<xsl:apply-templates select="."/>
</xsl:for-each>
OR
<xsl:for-each select="//chapter">
<!-- print the nodename -->
<xsl:value-of select="name()"/>
</xsl:for-each>
WHEREAS
<xsl:apply-templates select="//chapter">
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 -
http://xml.apache.org/xalan-j/
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 -
java org.apache.xalan.xslt.Process
and it should give you a list of options as shown below -
=================================================
-IN inputXMLURL
-XSL XSLTransformationURL
-OUT outputFileName
-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
false)
-RL recursionLimit (Set numeric limit on depht of stylesheet
recursion)
-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.
Thanks.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank u Jaydev excellent help. I succcessfully installed and tested my first app. I will for sure need more help and nice to know u are around.
Is there a example xslt programs that I can test some of the syntaxes like xsl aram etc etc?
thanx man!!!
 
Jayadev Pulaparty
Ranch Hand
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please check this tutorial out -
http://zvon.org/xxl/XSLTutorial/Output/contents.html
It teaches the concepts by examples.
 
reply
    Bookmark Topic Watch Topic
  • New Topic