Re: [Fwd: xsl:import (was Re: Multiple views on an xml document)]

Subject: Re: [Fwd: xsl:import (was Re: Multiple views on an xml document)]
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Tue, 7 Nov 2000 09:47:47 +0000
Lewis,

> Along these same lines, I have some html code I want to include in
> all of my html documents. A small example:
[amended example]
> <?xml version="1.0"?>
> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
version="1.0">>
>
> <style type="text/css" id="NOF_STYLE_SHEET">
>   DIV#Picture25LYR { position:absolute; visibility:inherit; }
>   DIV#Picture26LYR { position:absolute; visibility:inherit; }
>   DIV#Picture27LYR { position:absolute; visibility:inherit; }
> </style>
>
> </xsl:stylesheet>
>
> In the xsl file that creates my html, I include the line:
>
>   <xsl:import href="test.xsl"/>

xsl:import and xsl:include are designed to allow you to use the
variables, keys, templates and other XSLT constructs from a stylesheet
in another stylesheet, *not* to include a particular snippet of XML in
your stylesheet.

There are two ways of keeping XML snippets in separate files and
including them in your stylesheet. In both, the snippet is stored in a
separate file. So, you create an XML file that contains the text that
you want to include. For example, style.xml might be:

--- style.xml ---
<style type="text/css" id="NOF_STYLE_SHEET">
  DIV#Picture25LYR { position:absolute; visibility:inherit; }
  DIV#Picture26LYR { position:absolute; visibility:inherit; }
  DIV#Picture27LYR { position:absolute; visibility:inherit; }
</style>
---

The first is to use entities.  Within your stylesheet, declare the
entity within a DOCTYPE at the top of the page:

<!DOCTYPE xsl:stylesheet [
  <!ENTITY style SYSTEM 'style.xml'>
]>

Then, at the point where you want to include the contents of that
file, refer to the entity you've defined with an entity reference:

<xsl:template match="/">
  <html>
    <head>
      <title>My Page</title>
      <!-- CSS styles included here -->
      &style;
    </head>
    <body>
      ...
    </body>
  </html>
</xsl:template>

The contents of your style.xml file will then be included wherever you
reference the entity.

The second option is to use the document() function in XSLT to pull in
the contents of the style.xml file.  The call:

  document('style.xml')

accesses the XML document node, and you can then copy it to insert it
into your output:

<xsl:template match="/">
  <html>
    <head>
      <title>My Page</title>
      <!-- CSS styles included here -->
      <xsl:copy-of select="document('style.xml')/style" />
    </head>
    <body>
      ...
    </body>
  </html>
</xsl:template>

The two approaches each have advantages and disadvantages. The
entities approach is very useful for inserting any well-formed XML
into your stylesheet: it could be used to insert five elements, or a
piece of text, for example. With document(), on the other hand, the
XML being imported has to be XML, and has to be a *document* - that
means it has to have one, and only one, element. On the other hand,
the document() approach allows you more easily to *process* the
nodes that you pull in, which means that the included document can
itself be transformed before being outputted.

I hope that this helps,

Jeni

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



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


Current Thread