Help coderanch get a
new server
by contributing to the fundraiser
  • 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

question on apply-templates

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I have problems in using apply-templates
<!-- xml soure file bible01.xml-->
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="bible01_01.xsl"?>
<PERIODIC_TABLE>
<MARCO>
Display this.
<abc>polo</abc>
</MARCO>
<ATOM STATE="GAS">
<MELTING_POINT UNITS="Kelvin">0.95</MELTING_POINT>
<DENSITY UNITS="grams/cubic centimeter">0.0001785</DENSITY>
</ATOM>
</PERIODIC_TABLE>
//----------------------------------------------------------
//----------------------------------------------------------
<!-- xslt file bible01_01.xsl-->
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/PERIODIC_TABLE/MARCO">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
</xsl:stylesheet>

I use Xalan to process the two files.
java org.apache.xalan.xslt.Process -in bible01.xml -xsl bible01_01.xsl
//---------------------------------------------------
I DON'T KNOW WHY THE OUTPUT IS:
<?xml version="1.0" encoding="UTF-8"?>
<html>
Display this.
polo
</html>
0.95
0.0001785

WHY
0.95
0.0001785
COMES OUT IN THE OUTPUT,
BECAUSE I THOUGHT THAT I HAVE SET THE '/PERIODIC_TABLE/MARCO' (source element) in xsl file. THE OUTPUT SHOULD ONLY
INCLUDE
<html>
Display this.
polo
</html>
DO I HAVE SOME MISUNDERSTANDING ON THE SENTENCE FROM W3C:
The xsl:apply-templates element recursively processes the children of the source element.

I am so annoyed about using apply-templates. Is there any one know how to use apply-templates, or is there any tutorials in the
net?
thanks
Marco

[This message has been edited by Marco Yeung (edited October 08, 2001).]
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Marco!
The problem is not in apply-templates. How XSLT processor works? It walks through xml document and for each element it steps on it tried to find matching template. If there is no template with a "match" attribute aimed at the current element, so called "default" template will be applied.
Built-in default template for elements will output elements' content - and that's exactly what happens in your example.
When XSLT processor stands on <MELTING_POINT UNITS="Kelvin">0.95</MELTING_POINT> element, for example, it cannot find any matching template and aplies the default one. That's why you see 0.95 in the output.
To override this behaviour, you need to override the default templates. The easiest way is to write
<xsl:template match="text()"/>
this is an empty template, so no element content will ever be outputed "by default". For this reason you need to explicitely say that you want to see "MARCO" element content in the output. You can put xsl:value-of select element inside your template. So it would be:
<xsl:template match="/PERIODIC_TABLE/MARCO">
<html>
<xsl:value-of select="."/>
<xsl:apply-templates/>
</html>
</xsl:template>
Tutorials: there is Chapter 7 "Transforming XML with XSLT" from Steve Muench' "Building Oracle XML Applications" on-line and it's pretty good.
[This message has been edited by Mapraputa Is (edited October 10, 2001).]
 
Marco Yeung
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you for your help, Miss Mapraputa Is, your are really nice.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic