RE: [xsl] Help moving away from named templates

Subject: RE: [xsl] Help moving away from named templates
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 2 May 2007 18:02:43 +0100
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