xsl-list
[Top] [All Lists]

RE: alternative for modes

2004-02-12 15:31:47
Oki I think it's time for an example :) Imagine:

<streetrace>
        <car>
                <owner>...</owner>
                ...
        </car>
        <car>
                <owner>...</owner>
                ...
        </car>
        ...
</streetrace>

Now imagine you want to print out all the cars of the streetrace: first all
in red, then in blue, sorted by the owner of the car(in my example of
course, everything is a bit more complicated, especially the sorting code).
The best I came up with, is the following:

<xsl:template match="streetrace>
        <!-- print cars in blue !-->
        <xsl:call-template name="giveCarsSorted">
                <xsl:with-param name="mode" select="'blue'"/>
        </xsl:call-template>
        <!-- in red !-->
        <xsl:call-template name="giveCarsSorted">
                <xsl:with-param name="mode" select="'red'"/>
        </xsl:call-template>
</xsl:template>

<xsl:template name="giveCarsSorted">
        <xsl:param name="mode"/>

        <xsl:apply-templates> // this will go to each <car>-element
                <xsl:sort select="car/owner"/> // sort on <owner> in <car>
                <xsl:with-param name="mode" select="$mode"/> // propagate
$mode
        </xsl:apply-templates>
</xsl:template>

<xsl:template match="car">
        <xsl:param name="mode"/>

        <xsl:if test="$mode = 'blue'">
                <font color="blue"><xsl:value-of select="."/></font><br/>
        </xsl:if>
        <xsl:if test="$mode = 'red'">
                <font color="red"><xsl:value-of select="."/></font><br/>
        </xsl:if>
</xsl:template>

I hope I didn't make any big mistakes, since I haven't tested it myself.

In my opinion the problem is: I need the same sorting more than once, so
it's a good idea to put it in a separate template (the sorting in my code is
a bit more complicated). However, all the situations where I need the
sorting, DO NOT depend on any specific value(s) of the <car>-element or one
or more of his children. So I THINK creating 'conditional templates' won't
help since there is no condition, which can be deduced from the
tree-structure, at all.

Thanks for your time,

Peter

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Wendell Piez
Sent: donderdag 12 februari 2004 20:27
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: RE: [xsl] alternative for modes

Peter,

I don't know what you mean by "calls the other template on a particular 
order" (maybe typo for "in a particular order"?), but it may still be 
possible to do as Jeni says, since you can bind the result of a template 
call to a variable and interrogate it, as in:

<xsl:variable name="general-result">
   <xsl:call-template name="general"/>
</xsl:variable>

...then depending on what general-result is, you can do one thing or 
another (as well as copying it to the output).

This is all wonderfully vague: maybe seeing a concrete example would help? 
It's surprising how often straight conditionals can be avoided in XSLT by 
the smart use of templates ... as in

<xsl:apply-templates select="child[$condition1]" mode="condition1"/>
<xsl:apply-templates select="child[$condition2]" mode="condition2"/>

... and such like. Which seems to be in line with what you're asking.

Cheers,
Wendell

At 09:35 AM 2/12/2004, you wrote:
This is not possible I think, since the general template calls the other
template on a particular order (based on the calculation in the general
template), so I don't think it's possible in this situation.

Thanks for the tip,

Peter

-----Original Message-----
From: owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
[mailto:owner-xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com] On Behalf Of 
Jeni Tennison
Sent: donderdag 12 februari 2004 12:41
To: Peter Billen; xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: Re: [xsl] alternative for modes

Hi Peter,

As I mentioned above, 'generalTemplate' can be called on several
places in the template, because everytime I need the same
calculations, ... on the 'root'-tag. But each time (in every other
place), I actually need different manipulations on the 'child'-tag.

Just a thought, but depending on what the general template is doing,
it might be that you can turn around the processing, so that instead
of calling the general template with a parameter specifying the mode,
you apply templates in the specific mode and those templates call the
general template as appropriate to perform the necessary calculations.

Cheers,

Jeni


======================================================================
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


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