Re: [xsl] Help moving away from named templates

Subject: Re: [xsl] Help moving away from named templates
From: Steve <subsume@xxxxxxxxx>
Date: Wed, 26 Sep 2007 16:08:17 -0400
Way old thread, but thank you Michael, Abel, and David for your wisdom
concerning template matching. It was true, I was barely using the
potential of XML and my application rewrite from named to matched
templates is not only much more simple, but solved all kinds of
unexpected side-problems I thought were totally unrelated.

I do have a question for those of you who develop web applications,
especially if, like me, you're producing many instances of the same
application with minor custom modifications. Do you find that the
ability of your processing scripts to be re-useable yet override-able
lags behind your XSL structure?

-S

On 5/2/07, Michael Kay <mike@xxxxxxxxxxxx> wrote:
>
> There's nothing intrinsically bad about named templates. If your structure
> is completely regular and predictable (dare I say "relational") then there's
> nothing wrong with writing code that's committed to that structure, which is
> what tends to be the case when you use for-each and call-template (sometimes
> called "Pull" processing, though that term can confuse matters).
>
> What template rules and apply-templates give you is the ability to process
> data that's less regular and predictable, or that has variants and
> varieties. For example, it allows you to define a rule that will be used to
> process a firstName wherever it appears. If you find there's some context
> where a firstName appears and your rule isn't applicable, then you can
> refine the rule, for example you can have one rule for
> match="person[sex='M']/firstName" and another for
> match="person[sex='F']/firstName".
>
> You're starting with the assumption that each firstName is different and
> that defining a rule for processing firstName independently of its context
> doesn't make sense. If that's true of your data, then it's probably best not
> to use a template rule with match="firstName". Or perhaps you ought to be
> looking at whether you are using XML to its full potential: perhaps there
> are more opportunities for reusing both data structures and associated bits
> of code than you imagine. If there's nothing in common between the two
> elements, then why do they both have the same name? But if there is
> something in common, then there ought to be an opportunity for reusing code.
>
> Michael Kay
> http://www.saxonica.com/
>
>
> > -----Original Message-----
> > From: Steve [mailto:subsume@xxxxxxxxx]
> > Sent: 02 May 2007 16:18
> > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> > Subject: [xsl] Help moving away from named templates
> >
> > Hey there,
> >
> > I'm trying to reorganize my xsl application to make more use
> > of the template match attribute. I mostly transform XML
> > documents which are created based on SQL query results. For
> > the time being, my XSL calls a named template to display this
> > profile. I can think of two alternatives (illustrated below).
> > If someone has a better one I am all ears!
> >
> > Sample XML-------------
> >
> > <Records>
> >    <Record>
> >       <firstName>Steve</firstName>
> >       <customerID>999</custID>
> >    </Record>
> > </Records>
> >
> > --------------------------------
> >
> > 1) To display their profile I *could* do:
> >
> > <xsl:apply-templates
> > match="Records/Record/node()[name()='firstName']"/>
> >
> > and in most cases this would work, but of course this would
> > conflict with any other customer info that also contained
> > 'firstName' (display Emergency Contact info, for example,
> > would have a 'firstName' and thus conflict). I could make it
> > so it checked for more than the firstName, but this a
> > nightmare in the making. Am I wrong?
> >
> > 2) At the time of XML generation I could include a node
> > indicating which template needed to be displayed, but while
> > this simplifies XSL it doesn't seem to simplify program code.
> >
> > Example:
> >
> > <Records>
> >    <template>customerProfile</template>
> >    <Record>
> >           <etc />
> >           <etc />
> >    </Record>
> > </Records>
> >
> > Then do <xsl:apply-templates
> > match="Records[template='customerProfile']/Record" />
> >
> > The problem with this solution is that it doesn't seem to
> > reduce complexity, only to shuffle it away from XSL.
> >
> > Thanks!
> >
> > -Steve

Current Thread