Re: Embedding html in xml problem

Subject: Re: Embedding html in xml problem
From: Jeni Tennison <mail@xxxxxxxxxxxxxxxx>
Date: Sat, 02 Sep 2000 08:31:32 +0100
Kerry,

>Where I can put lots of html code between <top> and </top> (and all the rest
>too).  So in my xsl I can have something like:
>
>  <xsl:template match="top">
>    <table width="100%" border="0" height="30">
>      <tr>
>        <td>
>        <!-- whatever it takes to grab the values between the top tags,
>value-of or whatever -->
>        </td>
>      </tr>
>    </table>
>  <xsl:call-template name="left"/>
>  <xsl:call-template name="bottom"/>
>  </xsl:template>

You will find XSL a lot easier and more gratifying to use if you think in
terms of the abstract node trees that you're manipulating rather than the
string representation of those trees.  It is very very very rare that you
need to resort to CDATA sections and disable-output-escaping.

If you're wanting to grab all the content within the 'top' element and just
copy it node-for-node, then you're looking for xsl:copy-of.  xsl:copy-of
takes the nodes that you specify (so the HTML content of your 'top'
element) and copies them exactly as they are, with all their attributes and
all their content intact:

  <xsl:template match="top">
    <table width="100%" border="0" height="30">
      <tr>
        <td>
          <xsl:copy-of select="node()" />
        </td>
      </tr>
    </table>
    <xsl:call-template name="left"/>
    <xsl:call-template name="bottom"/>
  </xsl:template>

If the content of the 'top' element has some elements in it that you need
to process (e.g. embedded in the HTML, you have some XML elements that
indicate where the portal user's name should go) then you need to step
through the HTML, applying templates that, by default, copy the elements
and apply templates to their content, with exceptions for those embedded
XML elements:

<xsl:template match="*">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
  </xsl:copy>
</xsl:template>

<xsl:template match="insert-user">
  <xsl:value-of select="$user" />
</xsl:template>

Note that the default template (match="*") will match on *any* element.  It
may be worthwhile using namespaces to indicate which elements within your
input are HTML and which are XML, because you can then limit the copying
template to copying only the HTML elements:

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

Or the other alternative is to use 'modes' to limit the application of the
template.  So in your 'top' matching template have:

  <xsl:template match="top">
    <table width="100%" border="0" height="30">
      <tr>
        <td>
          <xsl:apply-templates mode="copy" />
        </td>
      </tr>
    </table>
    <xsl:call-template name="left"/>
    <xsl:call-template name="bottom"/>
  </xsl:template>

and make the copying template be both within that mode and apply templates
in that mode:

<xsl:template match="*" mode="copy">
  <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates mode="copy" />
  </xsl:copy>
</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