Re: XSL include problem

Subject: Re: XSL include problem
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Mon, 14 Aug 2000 18:50:00 +0100
At 12:45 14/08/00 +0100, Arthur Yeung wrote:
>    I have created my XML documents with schemas and XSLs. Is it possible to
>use 'include' method in XSL so I don't have to type the same piece of 'html'
>code in each XSL? Those 'html' code are static for all XSLs. E.g. header and
>footer.

At 15:58 14/08/00 +0530, Sunitha wrote:
>Could anyone help me out with my problem.
>I want to embed an HTML doucment(by giving the path of the file) into either
>an XSL or XML .
>Could you tell me how?

I'll answer these together since they are both the same question.

The first thing is that you need to make sure that your HTML files are
well-formed XML.  XSLT can't process anything that isn't well-formed XML.
You can make your HTML well-formed by running it through Tidy.

Once you have your HTML files well-formed you can include them into your
output in two main ways.

Firstly, you can declare them as external parsed entities within the
stylesheet and then put references to them wherever you want them put.  For
example:

<?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>Test Page</title></head>
    <body>
      <!-- includes header.html directly -->
      &header;
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

</xsl:stylesheet>

[See http://sources.redhat.com/ml/xsl-list/2000-07/msg00766.html for more
details.]

Secondly, you can access them using document and create copies of their
content.  For example:

<xsl:template match="/">
  <html>
    <head><title>Test Page</title></head>
    <body>
      <xsl:copy-of select="document('header.html')" />
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

The second method is probably better in most situations because it limits
the effective size of the stylesheet.  However, it can give quite verbose
output if you've declared the HTML namespace in your HTML (which you should
probably have done).  xsl:copy-of and xsl:copy both give complete copies of
all the nodes within the node set (i.e. the document in this case),
including all the namespace nodes, so you get
xmlns="http://www.w3.org/1999/xhtml"; on every element.

A final approach is to have a couple of templates that copy the elements by
hand (as it were) and thus don't include the namespace (best to define a
separate mode to do this):

<xsl:template match="*" mode="copy">
  <xsl:element name="{local-name()}">
    <xsl:copy-of select="@*" />
    <xsl:apply-templates mode="copy" />
  </xsl:element>
</xsl:template>

<xsl:template match="text()" mode="copy">
  <xsl:value-of select="." />
</xsl:template>

And then apply templates to the document in copy mode:

<xsl:template match="/">
  <html>
    <head><title>Test Page</title></head>
    <body>
      <xsl:apply-templates select="document('header.html')" mode="copy" />
      <xsl:apply-templates />
    </body>
  </html>
</xsl:template>

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