[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: Template Question

Subject: Re: Template Question
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Thu, 27 Apr 2000 16:44:42 +0100
Re: Template Question
Hi Anis,

The reason this isn't working for you--

><xsl:template name="Chap3">
><Chap3Text><xsl:value-of select="."/></Chap3text><BR>

is that the template is copying into your result tree the string value of
the node matched ... which is to say, all its text content, but providing
special handling for none of the elements below it in the hierarchy.

Turn it and the others into

<xsl:template name="Chap3">

and you should be in business.

All your HL elements will be changed into B elements by your rule,

><xsl:template match="HL">
><B><xsl:value-of select="."/></B>

although once again this will not provide for processing of any descendant
elements of HL. Change this one to <xsl:apply-templates/>, too, if you want
to support that.

In either case, your stylesheet should then do what you want, descending
through the entire source tree and firing off whichever templates match the
elements found, at the appropriate times. You will find this to be very
efficient -- and even more so if your XML is designed with clean element
typing (e.g. if your elements were named <Chap n="1">...</Chap>...<Chap
n="4">...</Chap> you could use an <xsl:sort> to achieve what you want and
avoid writing several templates -- you wouldn't need any named templates at
all, since it would all happen through match patterns).

For comprehension purposes, it may help to know that by default,
<xsl:apply-templates/> selects all the element children of the context
node, and processes them using whichever templates in the stylesheet match.
This recursive processing makes for very simple specification of
straightforward transformations. You can also say <xsl:apply-templates
select="Chapter"/> (using the XPath expression language) if, for example,
you only wanted to process the element children of type 'Chapter'.

Try it out and see if the lights go on ... most new users of XSL have to
have an "aha!" or two. Hoping that helps,

Wendell Piez

At 02:36 PM 4/27/00 -0500, you wrote:
>Please consider the following XML and XSL:
><Title> Cat And Dog</Title>
><Chap1>The big<HL>dog</HL> and the little cat</Chap1>
><Chap3>The cat is little and black</Chap3>
><Chap2>The <HL>dog</HL> is big and brown</Chap2>
><Chap4>The big <HL>dog</HL>....</Chap4>
><?xml version='1.0'?>
><xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
><xsl:output method='xml' indent='yes'/>
><xsl:template match="*|/"><xsl:apply-templates/></xsl:template>
><xsl:template match="text()|@*"><xsl:value-of select="."/></xsl:template>
><xsl:template match="/">
><xsl:template match="Book">
><xsl:call-template name="Chap1"/>
><xsl:call-template name="Chap2"/>
><xsl:call-template name="Chap3"/>
><xsl:call-template name="Chap4"/>
><xsl:template name="Chap1">
><Chap1Text><xsl:value-of select="."/></Chap1text><BR>
><xsl:template name="Chap2">
><Chap2Text><xsl:value-of select="."/></Chap2text><BR>
><xsl:template name="Chap3">
><Chap3Text><xsl:value-of select="."/></Chap3text><BR>
><xsl:template name="Chap4">
><Chap4Text><xsl:value-of select="."/></Chap4text><BR>
><xsl:template match="HL">
><B><xsl:value-of select="."/></B>
>I would like to see the following output:
>	<Chap1text>The big<B>dog</B> and the little cat</Chap1text><BR>
><Chap2text>The <B>dog</B> is big and brown</Chap2text><BR>
>	<Chap3text>The cat is little and black</Chap3text><BR>
><Chap4text>The big <B>dog</B>....</Chap4text><BR>

Wendell Piez                            mailto:wapiez@xxxxxxxxxxxxxxxx
Mulberry Technologies, Inc.                http://www.mulberrytech.com
17 West Jefferson Street                    Direct Phone: 301/315-9635
Suite 207                                          Phone: 301/315-9631
Rockville, MD  20850                                 Fax: 301/315-8285
  Mulberry Technologies: A Consultancy Specializing in SGML and XML

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list

Current Thread


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.