wood burning stoves 2.0*
The moose likes Product and Other Certifications and the fly likes simple example problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Product and Other Certifications
Bookmark "simple example problem" Watch "simple example problem" New topic
Author

simple example problem

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
<source>
<title>XSL</title>
<author>John Smith</author>
<auth>John Smith</auth>
<author>John Smith</author>
</source>
********************************
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="title">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="author">
<h2><xsl:apply-templates/></h2>
</xsl:template>
</xsl:stylesheet>
o/p i get is
<?xml version="1.0" encoding="UTF-8"?>
<h1>XSL</h1>
<h2>John Smith</h2>
John Smith -----why is this getting printed???
<h2>John Smith</h2>
****************************
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
http://www.zvon.org/xxl/XSLTutorial/Output/example4_ch2.html
<xsl:apply-templates select="/source//AAA/BBB//*"/>
what does this do?
Jayadev Pulaparty
Ranch Hand

Joined: Mar 25, 2002
Posts: 662
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: simple example problem
 
Similar Threads
Need Help with XSLT
filter data while parsing
Mock exam question
Extracting XSL content to XML with XSLT problem ...
Using XSL to display XML