This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

problem in template matching

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,
I m new to this group and also to XML and aspire for the certification. I m unable to understand the outcome of this.The xml file is as under.
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="template1.xsl"?>
<source>
<source1>
<element1>element1</element1>
<element3>element3</element3>
<element2>element2</element2>
</source1>
</source>
The XSL is as under:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="source/source1/element1">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
<xsl:template match="element3">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
</xsl:stylesheet>
However the output is:
element1element3element2
with element1element3 in italics.
I fail to understand why "element2" appears in the output.
Thanx in advance
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look at - https://coderanch.com/t/146692/po/certification/Default-processing
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess it could be because, the default template rule is fired. The rule being

<xsl:template match="/|*">
<xsl:apply-templates/>
</xsl:template>
Hence apply-templates, executes the child node of curent node. When element3 is matched that corresponding template rule is fired. For element2, the default template rule for text() is fired.
That is why, element1 and element3 are in italics while element2 is not.
Experts please correct me if Iam wrong
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, does the processor eventually get to the stage where it says " do i have templates for children of "source1", finds it has 3 - then uses the two author defined templates for element1 and element3, and finally, uses the built in rule for the third child element2?
I looked at it for ages before finally seeing something logical happening behind the scenes. I hope this is whats happening - otherwise it makes no sense to me either!!
Andles.
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The XSLT processor looks for a template matching "/". Not finding one, it applies the default rule to "/", which is to apply templates to each child. In this case, there is one child, "source".
The XSLT processor also finds no template matching
"source", so it continues recursing, looking for a template to apply to each of the children. In this case, there is one child, "source1".
The XSLT processor again finds no template matching
"/source/source1", so it continues recursing, looking for templates to apply to each of the children:
"element1", "element3", and "element2".
It finds a template matching "source/source1/element1", so it applies that template. It also find a rule matching "element3", so it next applies that template. It does NOT find a rule matching "element2", so it applies the default template, which is to just print out the element's text value.
 
Bunny Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanx for the responses. Given under are the default templates in case a server applies an XSLT stylesheet to an XML Document. to transform to some other format.

<xsl:template name="get-nested-content">
<xsl aram name="content"/>
<xsl:choose>
<xsl:when test="$content/*">
<xsl:apply-templates
select="$content/*"/>
</xsl:when>
<xsl therwise>
"<xsl:value-of select="$content"/>"
</xsl therwise>
</xsl:choose>
</xsl:template>
<xsl:template match="@*|*|text()|processing-
instruction()">
<xsl:copy>
<xsl:apply-templates select="@*|*|text()
|processing-instruction()"/>
</xsl:copy>
</xsl:template>
what do the templates(XPATH Part) mean. What I infer of the second template is that either the child with an attribute or any child .....
when already the condition for "all children" is specified what is the need for "@*" condition specifying child with attribute.
Plz throw some light on this.
Bunny Singh
 
Bunny Singh
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also what all are the default templates in case the XML Document and the associated styleshet are both served to the client(Web Browser) directly.
Rgds,
Bunny Singh
 
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic