xsl-list
[Top] [All Lists]

Re: decorator / wrapper design pattern

2005-03-18 09:31:59
Hi Lucas,

I'm not sure whether this could rightly be said to map to the "decorator" pattern (maybe so), but it seems to me you may be looking for the idiom:

<xsl:template match="something">
  <xsl:call-template name="some-wrapper"/>
</xsl:template>

<xsl:template name="some-wrapper">
  <xsl:param name="contents">
    <xsl:apply-templates/>
  </xsl:param>
  <wrapper>
    <xsl:copy-of select="$contents"/>
  </wrapper>
</xsl:template>

this allows you also to do things like

<xsl:template match="something-else">
  <xsl:call-template name="some-wrapper">
    <xsl:with-param select="contents">
      <xsl:call-template name="some-other-wrapper"/>
    </xsl:with-param>
  </xsl:call-template>
</xsl:template>

or even

<xsl:template match="another-something-else">
  <xsl:call-template name="some-wrapper">
    <xsl:with-param select="contents">
      <xsl:text>Wrap me!</xsl:text>
    </xsl:with-param>
  </xsl:call-template>
  <xsl:apply-templates/>
</xsl:template>

That is, the "wrapper" (or decorator) template can wrap anything you pass it.

A "thick" solution to a nasty problem (map a set of attributes to a set of wrappers) using this idiom (although by matching templates, not calling them by name) appears at

But I should also caution you that it isn't very common to have to do this; lighter-weight solutions are usually possible. Also, as Jay says, the requirements this addresses are often better answered by pushing the problem into the next layer up, for example into a CSS stylesheet to be applied to HTML output.

Enjoy!
Wendell


At 08:37 AM 3/18/2005, you 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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--


======================================================================
Wendell Piez                            
mailto:wapiez(_at_)mulberrytech(_dot_)com
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
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>