Re: [xsl] decorator / wrapper design pattern

Subject: Re: [xsl] decorator / wrapper design pattern
From: Stef <stef@xxxxxxxxx>
Date: Fri, 18 Mar 2005 10:17:38 -0500
Hello again Lucas,
	I understand your needs, you want to apply mutliple
tags, in which case, have a look at the link I provided. Right
down at the bottom is XSLT 1.0 code, which is probably what
you want.

	I gave you my (*knowingly broken*) stylesheet to try
and illustrate that (as is my understanding) a template will
applied based on how exact a match is. Chaining the templates
is usually fraught with, well, rather ... 'ugly' code.
At least to my eye's (and this from a perl programmer ;)

	regards
	Stef

On Fri, Mar 18, 2005 at 04:08:47PM +0100, Schaik, L.B. van wrote:
> Hi,
> 
> Thanks Stef for this solution, but what I want is a little different:
> In the example, it is clear that I want title and paragraph formatted
> this way. But in fact I don't now before hand what the elements are that
> need italic or bold. So I want to make templates (or something) that
> surround some context with some elements. Then I want to call these
> templates with a node that is then wrapped.
> In a C like programming environment, this is what I want:
> 
> function bold(var content) {
>   return "<B>"+content+"</B>";
> }
> 
> function italic(var content) {
>   return "<I>"+content+"</I>";
> }
> 
> print bold(italic(some node));
> 
> I hope this is more clear.
> 
> greetings and thanks,
> Lucas
> 
> -----Original Message-----
> From: Stef [mailto:stef@xxxxxxxxx] 
> Sent: vrijdag 18 maart 2005 16:01
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [SPAM-BA] - Re: [xsl] decorator / wrapper design pattern -
> Bayesian Filter detected spam
> 
> 
> Hello Lucas,
> 	It sounds (to me at least) very much like you want to apply
> mutliple tags via XSLT. There is a really great write-up of how someone
> solved this on the following link;
> 
> 	http://www.dpawson.co.uk/xsl/sect2/multitags.html
> 
> 	Its a lot more in-depth than any solution I could give, and
> infact, I merely (when I need to do this) exactly specify something
> like;
> 
> <xsl:stylesheet
>   xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
>   version="1.0">
> 
> <xsl:template match="/">
>         <xsl:apply-templates />
> </xsl:template>
> 
> <xsl:template match="title|paragraph">
>   <i><xsl:apply-templates /></i>
> </xsl:template>
> 
> <xsl:template match="title">
>   <i><b><xsl:apply-templates /></b></i>
> </xsl:template>
> 
> </xsl:stylesheet>
> 
> 
> 	use that and you should get a hint as to how apply-templates
> works on matches ;>
> 
> 	regards
> 	Stef
> (ps. I am still learning this crazy thing called XSLT, so any mistakes,
> feel free to scream and point :)
> 
> On Fri, Mar 18, 2005 at 02:37:10PM +0100, Schaik, L.B. van wrote:
> > Hi,
> > 
> > I'm using XSL version 1.0 and am trying to use the decorator design 
> > pattern, but can't get it to work in XSL.
> > Here is what I try to achieve:
> > Different types of elements all need to be surrounded by the same
> text.
> > For example:
> > 
> > I've XML:
> > <base>
> >   <section>
> >      <title>the title</title>
> >      <paragraph>some text</paragraph>
> >      <paragraph>more text</paragraph>
> >   </section>
> >   <section>
> >      <title>the title</title>
> >      <paragraph>other text</paragraph>
> >      <paragraph>more other text</paragraph>
> >      <paragraph>and more text</paragraph>
> >   </section>
> > </base>
> > 
> > Now I want all text in titles and in paragraphs to be italic and in 
> > titles also to be bold.
> > My first thought was to use call-template like this:
> > <xsl:template match="title">
> >   <xsl:call-template name="bold">
> >     <xsl:call-template name="italic">
> >        <xsl:value-of select="."/>
> >     </xsl:call-template>
> >   </xsl:call-template>
> > </xsl:template>
> > 
> > But this is not the way to do this.
> > I cannot however figure out or find the solution. The decoration that 
> > must be applied is much more complicated than bold or italic, but that
> 
> > is besides the point, I think.
> > Here is my second try, but here is the problem that I cannot nest 
> > decorators, so bold and italic is not possible on the same element:
> > 
> > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
> > version="1.0">
> > 
> > <xsl:template match="/">
> >   <xsl:call-template name="body"/>
> > </xsl:template>
> > 
> > <xsl:template match="section">
> >   <xsl:apply-templates />
> >   <br/>
> > </xsl:template>
> > 
> > <xsl:template match="title">
> >   <xsl:call-template name="bold" />
> >   <br/>
> > </xsl:template>
> > 
> > <xsl:template match="paragraph">
> >   <xsl:call-template name="italic" />
> > </xsl:template>
> > 
> > <xsl:template name="bold">
> >   <b>
> >      <xsl:apply-templates/>
> >   </b>
> > </xsl:template>
> > 
> > <xsl:template name="italic">
> >   <i>
> >      <xsl:apply-templates/>
> >   </i>
> > </xsl:template>
> > 
> > <xsl:template name="body">
> >   <html>
> >     <body>
> >       <xsl:apply-templates />
> >     </body>
> >   </html>
> > </xsl:template>
> > 
> > </xsl:stylesheet>
> > 
> > 
> > Can anybody give me a pointer?
> > 
> > Thanks in advance
> > 
> > Lucas

Current Thread