Re: [xsl] Calling a different stylesheet based upon XML tag?

Subject: Re: [xsl] Calling a different stylesheet based upon XML tag?
From: Jeni Tennison <jeni@xxxxxxxxxxxxxxxx>
Date: Tue, 26 Oct 2004 15:08:38 +0100
Hi Rebecca,

> See at the moment I have a file like order.xslt and within it there is a
> a template match="Address" with a set of instructions.  I also have a
> file like bill.xslt and within it there is also a template
> match="Address" with a different set of instructions.  
[snip]
> Is there a way to create an input driver xslt file and an output xslt
> driver file where it either includes just the one set of templates I
> need at run time.  Or where it can point the text to transform to the
> correct file?

There's no way to say "only invoke/call templates from this
stylesheet" in XSLT.

I can think of two approaches for the input (where you have matching,
rather than named, templates), but both require you to change
order.xslt and bill.xslt to get it to work.

First, you could qualify the match patterns in your templates so that
they take into account where the Address (for example) was found, thus
avoiding clashes between the templates. For example, use
"Bill//Address" in bill.xslt and "Order//Address" in order.xslt.

Alternatively, you could add modes to all the templates in your
stylesheet: put the templates in order.xslt in 'order' mode and the
templates in bill.xslt in 'bill' mode. Then, in the driver stylesheet,
apply templates in the appropriate mode based on the input XML. (I
tend to use modes everywhere in my stylesheets precisely to make this
kind of merger easier.)

On the output, where you're using named templates instead, your only
real option is to rename the templates to avoid naming conflicts, and
call the appropriate ones. Or you could redesign the stylesheets to
use matching templates instead (matching templates are just as good as
named templates for constraining the order of the output), and use one
of the techniques above.

Of course, rather than merging the stylesheets, you could use external
code to determine which stylesheet should be invoked based on the
source XML. This is probably the easiest option, if you have control
over the code that's used to invoke the transformations.

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Current Thread