Re: [xsl] using document() to read HTML

Subject: Re: [xsl] using document() to read HTML
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 3 Feb 2001 11:58:54 +0000
Hi Rosa,

> thanx to David for the hyperlink replies. Here's another one: when u
> read in a HTML page say using document(top.htm) - don't know any
> other way - , I presume it's like a pointer pointing to the
> beginning node of that file. Because I want sections of the HTM file
> or even the whole HTM file to be in front of some HTML which will be
> translated by XSL.

If you do:

  <xsl:copy-of select="document('top.htm')" />

then you will get a copy of the entire file at the point where you
place the instruction.

If you're interested in a specific part of that document, then you can
select nodes in it using an XPath in just the same way as you can
select nodes in the source XML document.  For example:

  <xsl:copy-of select="document('top.htm')/*/BODY/TABLE[1]/TR" />

will copy all the TR elements of the first TABLE element within the
BODY element of your HTML document.

If you want to determine what to copy on the fly, then you can apply
templates to the external HTML document in just the same way as you
can to the source document.  So you could do:

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

and then have templates that perform the relevant copying:

<xsl:template match="TR">
   <xsl:copy-of select="." />

One thing to watch out for if you take this last approach is that
there might be clashes between templates you're using for the main
source XML document and the templates you want to use for the external
HTML document. If this is a problem, it might be best to use modes to
distinguish between the two.  For example:

  <xsl:apply-templates select="document('top.htm')/*" mode="insert" />

and then:

<xsl:template match="TR" mode="insert">
   <xsl:copy-of select="." />

I hope that helps,


Jeni Tennison

 XSL-List info and archive:

Current Thread