Re: Am I missing something?

Subject: Re: Am I missing something?
From: Steve Tinney <stinney@xxxxxxxxxxxxx>
Date: Thu, 20 Jan 2000 22:16:50 -0500
"Kirk V. Hastings" wrote:
> But for the life of me, I don't see how to do it with XSLT. The following
> template makes XT see red:
> 
> <xsl:template match="pb">
>    </table>
>    <table>
>      <tr><th colspan="2">Page <xsl:value-of select="@n"/></th></tr>
> </xsl:template>

Nope, can't do that.  The XSL stylesheet has to be a well-formed XML
document.  Nesting </table> inside <xsl:template> is not WF (FAQ
abbreviation?).

To solve this kind of problem you need to control the instantiation
sequence more tightly.  The following does what you ask (and should
scale to more than 2 columns) by selecting at each point specific nodes
to have templates applied to them.  The only bit that might be a bit
obscure is the selection criterion for cb; the code says only to grab
the 'cb' elements whose preceding 'pb' element is the one currently
being processed (determined by its position among the siblings).  You
could embed the $pos operand's logic in the test statement using the
current() function and avoid the variable, but I find that using
variables for these things is clearer.

 Steve

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="html"/>

<xsl:template match="/">
  <xsl:apply-templates/>
</xsl:template>

<xsl:template match="div">
  <div class="{@type}">
    <xsl:apply-templates select="pb"/>
  </div>
</xsl:template>

<xsl:template match="pb">
  <table class="pb">
    <tr><th colspan="2">Page <xsl:value-of select="@n"/></th></tr>
    <tr>
      <xsl:variable name="pos" 
                    select="1+count(preceding-sibling::pb)"/>
      <xsl:apply-templates 
       select="following-sibling::cb
               [count(preceding-sibling::pb) = $pos]"/>
    </tr>
  </table>
</xsl:template>

<xsl:template match="cb">
  <td class="cb">
    <xsl:apply-templates select="following-sibling::p[1]"/>
  </td>
</xsl:template>

<xsl:template match="p">
  <p><xsl:apply-templates/></p>
</xsl:template>

</xsl:stylesheet>


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


Current Thread