Re: [xsl] Disable Output Esacping Problems

Subject: Re: [xsl] Disable Output Esacping Problems
From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx>
Date: Wed, 06 Nov 2002 16:20:29 -0500
Hi Rechell,

At 03:17 PM 11/6/2002, you wrote:
I am trying to generate an HTML file that will really be generated as an XML file and then be used as input to another XSL stylesheet (for styling purposes) where it will then be converted to a new HTML file for display in the browser. In the second XSLT file, the program goes through each <TR> and <TD> tag and adds an appropriate atribute for styling.

Sounds fine.


In the first XSLT file I need (or I think I need) to disable output esacping because I need to generate non-symmetrical XML tags. The disabling of output esacping generates the expected output, but the second XSLT file doesn't seem to recognize the <TD> and <TR> tags whose output was escaped and fails to apply the appropriate styling to these tags.

Right. You are discovering why disable-output-escaping isn't a solution (though it may seem like one). Unless you serialize the output of the first process and then run the second one off the serialization, your chain won't work since (as you're discovering) disable output escaping doesn't really create nodes in the output tree.


I can say with some assurance, however, that you don't really need d-o-e. Whenever you run into a spot where you "need to generate non-symmetical XML tags", invariably if you pull back one step, you find there's a spot where the same "tags" can be created without breaking the requirement for symmetry.

A typical example might look like this. Your input looks like

<group>
<item>Item one</item>
<item>Item two</item>
<item>Item three</item>
<item>Item four</item>
</group>

And you want to make a table like this:

<table>
  <tr><td>Item one</td><td>Item two</td></tr>
  <tr><td>Item three</td><td>Item four</td></tr>
</table>

A d-o-e-based "solution" looks like this:

<xsl:template match="group">
  <table>
    <xsl:apply-templates select="item"/>
  </table>
</xsl:template>

<xsl:template match="item">
  <xsl:if test="(position() mod 2) = 1">
    <!-- create a tr tag before an odd item -->
    <xsl:text disable-output-escaping="yes">&lt;tr&gt;</xsl:text>
  </xsl:if>
  <td>
    <xsl:apply-templates/>
  </td>
  <xsl:if test="(position() mod 2) != 1">
    <!-- create a /tr tag after an even item -->
    <xsl:text disable-output-escaping="yes">&lt;/tr&gt;</xsl:text>
  </xsl:if>
</xsl:template>

I have no doubt that whoever thought of this figured they were being really smart to get around a perceived "weakness" in the language ... that it can't create output that's not well-formed without going to extra trouble. Yet this weakness is actually a strength: XSLT is not a language for describing manipulations of strings (and tags are after all just strings), but for describing rearrangements and relabellings of information in hierarchical organizations of nodes. There are all kinds of painful and tedious things we don't have to worry about as a consequence.

The correct solution, therefore, is to let the language handle nodes:

<xsl:template match="item">
  <xsl:if test="(position() mod 2) = 1">
  <!-- we only create output for odd items -->
    <tr>
      <td>
      <!-- a table cell for the odd item -->
        <xsl:apply-templates/>
      </td>
      <td>
      <!-- a table cell for the even item following -->
        <xsl:apply-templates select="following-sibling::item[1]/node()"/>
      </td>
    </tr>
  </xsl:if>
</xsl:template>

There many forms and arrangements of templates possible that take this same basic approach. No d-o-e is called for, and thus having the output you want is not dependent on serialization, and you can chain your transforms however you need to, to your heart's conten, without ever writing a file.

I hope this helps. If you can't see your way to a solution based on this simple example, post a bit more of your source data and desired output, and someone will show how it can be done in your case.

Cheers,
Wendell


====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================


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



Current Thread