xsl-list
[Top] [All Lists]

RE: xsl:apply-templates behaving differently depending on the node copied?

2005-05-11 05:02:46
On your XML (corrected for markup), the following XSL shoukld illustrate the idea:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
       version="1.0">
   <xsl:output method="html" />

   <xsl:template match="node( ) | @*">
       <xsl:copy>
           <xsl:apply-templates select="@* | node( )"/>
       </xsl:copy>
   </xsl:template>

   <xsl:template match="td[count(text()) = 0]">
       <td>
           <xsl:text>__NOTHING__</xsl:text>
           <xsl:apply-templates/>
       </td>
   </xsl:template>

</xsl:stylesheet>

It uses an identity transform to copy everything verbatim, except for empty <td> elements, which are filled-in with default text. You should be able to use some variation of this for what you ultimately need.

Of course, we were lucky here in that we could specifically match empty td elements. Things would not have been quite so easy otherwise. For example, XSL doesn't have a provision for invoking a template on a non-match--there was a thread about just that about 2 months ago.

Regards,

--A

From: knocte <knocte(_at_)gmail(_dot_)com>

<page>
  <head>
    <title>Example</title>
  </head>

  <content>
    <h1>Example</h1>
    <table>
      <tr>
        <td></td>
        <td>Something #1</td>
      </tr>
      <tr>
        <td>Somthing #2</td>
        <td />
      </tr>
    </table>
  <content>
</page>


And it will convert it to the following:

<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <div id="content">
      <h1>Example</h1>
      <table>
        <tr>
          <td></td>
          <td>Something #1</td>
        </tr>
        <tr>
          <td>Something #2</td>
          <td />
        </tr>
      </table>
    </div>
  <body>
</html>


But how can I use apply-templates to obtain the following?:

<html>
  <head>
    <title>Example</title>
  </head>
  <body>
    <div id="content">
      <h1>Example</h1>
      <table>
        <tr>
          <td>________NOTHING!_______</td>
          <td>Something #1</td>
        </tr>
        <tr>
          <td>Something #2</td>
          <td>________NOTHING!_______</td>
        </tr>
      </table>
    </div>
  <body>
</html>

_________________________________________________________________
Don?t just search. Find. Check out the new MSN Search! http://search.msn.click-url.com/go/onm00200636ave/direct/01/


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--