Re: how to implement different xsl in one document

Subject: Re: how to implement different xsl in one document
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 28 Nov 2000 14:40:05 +0000
Elena,

> I have the following problem: I have three different xml/xsl files,
> one for menu above, one for navigation, one for holder. Now I'd like
> to put everything together in one file. Can I make it with the help
> of 'xsl:include'? If yes, how? Can I connect three different xsl in
> one file?

You can use xsl:include to combine the three different XSLT
stylesheets and use the document() function to pull in the different
XML files.

To include, say, menu.xsl within your single stylesheet file, use:

  <xsl:include href="menu.xsl" />

To apply templates to, say, menu.xml within your single stylesheet
file, use something like:

  <xsl:apply-templates select="document('menu.xml')/*" />

The thing that you have to be careful about is clashes between the
various templates and how they will interact with each other. If you
have named templates, then make sure that they're named differently or
that they do the same thing in the different stylesheets. If you have
unnamed templates, then you may well have to use modes to ensure that
the correct one is used, unless the XML source is completely different
for each of the stylesheets. To use modes, add a 'mode' attribute to
the template:

<xsl:template match="..." mode="menu">
  ...
</xsl:template>

and include that mode wherever you have an explicit
xsl:apply-templates instruction:

  <xsl:apply-templates mode="menu" />

In particular you may run into problems with having different
templates in each of the stylesheets, each of which match the root
node, i.e.:

<xsl:template match="/">
  ...
</xsl:template>

If you do need to use modes, then you need to make sure that you have
a place where templates start being applied in that mode.  If that's
the case, I would change all the unnamed templates in a particular
stylesheet to use that particular mode, including those that don't
clash with others, and the one for the root node, and then have a
root-node-matching template in the master stylesheet to kick off the
processing in the right modes, looking something like:

<xsl:template match="/">
  ...
  <xsl:apply-templates select="document('menu.xml')"
                       mode="menu" />
  ...
  <xsl:apply-templates select="document('navigation.xml')"
                       mode="navigation" />
  ...
</xsl:template>

I hope that helps,

Jeni

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



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


Current Thread