Altova Mailing List Archives


Re: Embedding html in xml problem

From: Jeni Tennison <mail@---------------->
To:
Date: 9/2/2000 9:32:00 AM
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

Disclaimer

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.