RE: multiple stylesheets in one

Subject: RE: multiple stylesheets in one
From: Matt Watson <Matt.Watson@xxxxxxxxxxxxxxxxxx>
Date: Mon, 24 Jul 2000 10:32:44 +0100
Thanks very much for your extremely helpful response.

cheers,
-matt

-----Original Message-----
From: Jeni Tennison [mailto:jeni@xxxxxxxxxxxxxxxx]
Sent: 21 July 2000 16:59
To: Matt Watson
Cc: 'xsl-list@xxxxxxxxxxxxxxxx'
Subject: Re: multiple stylesheets in one


Matt,

>What I want to test - I'm a nembie - is combining multiple stylesheets into
>one document. I have seen some references to this in the nulberrytech lists
>but nothing specifically seems to answer my question.

I'm a bit confused over what exactly you want to do.  There are three types
of combinations that you might be interested in:

1. you have additional HTML files that you want to include in the document
you're producing
2. you have additional XML files that you want to transform and include in
the document you're producing
3. you have additional XSLT files that you want to use to transform your
input

I think that you want to do Option 1, but just in case I'll explain each of
these in turn, looking only at including the header for brevity (the same
principals apply to the footer).

In the first case, you should have a file called something like header.html
that contains the menu that you want to include in your output, in exactly
the form you want to include it in your output:

---- header.html ----
<table>
  <tr>
    <td><a href="/">Home</a></td>
    <td><a href="/movies/">Movies</a></td>
    <td><a href="/shop/">Shop</a></td>
  </tr>
</table>
----

In this case, probably the simplest way to get this into your output is not
to use any XSLT jiggerypokery, but to simply include it as an external
parsed entity in the stylesheet.  This involves declaring and referencing
the entity within your stylesheet:

---- data.xsl ----
<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
<!-- declares header.html as an external parsed entity -->
<!ENTITY header SYSTEM "header.html">
]>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
  <html>
    <head><title>People</title></head>
    <body>
      <!-- includes header.html directly -->
      &header;
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>
----

In the second case, you should have a file called something like header.xml
that contains an XML representation of the menu that you want to include in
your output:

---- header.xml ----
<menu>
  <item href="/">Home</item>
  <item href="/movies/">Movies</item>
  <item href="/shop/">Shop</item>
</menu>
----

In this case, you need to use the document() function to access this
information and you need to have templates in your stylesheet to transform
it and include it in your output:

---- data.xsl ----
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="/">
  <html>
    <head><title>People</title></head>
    <body>
      <!-- applies templates to the information contained in header.xml -->
      <xsl:apply-templates select="document('header.xml')" />
      <!-- applies templates to the input file -->
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

<!-- transforms the XML in header.xml into the table we want -->
<xsl:template match="menu">
  <table>
    <tr>
      <xsl:for-each select="item">
        <td><a href="{@href}"><xsl:value-of select="." /></a></tr>
      </xsl:for-each>
    </tr>
  </table>
</xsl:template>

</xsl:stylesheet>
----

In the third case, you should have an input XML document that includes some
information about the menu that you want as well as the data for the rest
of the page, something like:

---- data.xml ----
<?xml version="1.0"?>
<doc>
<menu>
  <item href="/">Home</item>
  <item href="/movies/">Movies</item>
  <item href="/shop/">Shop</item>
</menu>
<people>
  <person age="50" name="larry"/>
  <person age="50" name="larry"/>
</people>
</doc>
----

You should also have a stylesheet that contains templates to transform this
header information into the output that you want:

---- header.xsl ----
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:template match="menu">
  <table>
    <tr>
      <xsl:for-each select="item">
        <td><a href="{@href}"><xsl:value-of select="." /></a></tr>
      </xsl:for-each>
    </tr>
  </table>
</xsl:template>

</xsl:stylesheet>
----

In this case, you want to import or include this stylesheet so that the
templates that are defined within it are processed as if they were part of
your general stylesheet.  Whether you want to use xsl:import or xsl:include
depends on whether you want to do anything funny in terms of overriding
(some of) the templates that are defined in the imported stylesheet: if you
do, then use xsl:import, otherwise, use xsl:include:

---- data.xsl ----
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<!-- includes the templates from the header.xsl stylesheet -->
<xsl:include href="header.xsl" />

<xsl:template match="/">
  <html>
    <head><title>People</title></head>
    <body>
      <!-- applies templates to the menu definition to create the header
           - the templates come from header.xsl -->
      <xsl:apply-templates select="doc/menu" />
      <!-- applies templates to the data to create the rest of the document
-->
      <xsl:apply-templates select="doc/people" />
    </body>
  </html>
</xsl:template>

...

</xsl:stylesheet>
----

Of course you can mix and match these: using included templates to process
external information accessed through the document() function, for example.

I hope that this gives you some ideas about how to do what you want to do,
anyway.

Cheers,

Jeni

Dr Jeni Tennison
Epistemics Ltd * Strelley Hall * Nottingham * NG8 6PE
tel: 0115 906 1301 * fax: 0115 906 1304 * email:
jeni.tennison@xxxxxxxxxxxxxxxx


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


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


Current Thread