• 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

simple example problem

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<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>
****************************
 
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,
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
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 662
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
today's feeble attempt to support the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic