Re: [xsl] Counting nodes processed

Subject: Re: [xsl] Counting nodes processed
From: "Thomas B. Passin" <tpassin@xxxxxxxxxxxx>
Date: Mon, 4 Mar 2002 16:18:29 -0500
[JAY SCHERER]

> This works fine except that I really only want to print the category 1
time, not each line.  In the previous solution I was able to test on the
position in the topic to eliminate the category header when not the 1st
time.  Any hints?
>

Minor changes.  If you want to show the category names like that, you should
iterate over the category elements. Output the category name once for each
iteration.  You have to restrict the allowed topics further, to ones whose
parent category element is the current one.  Mike Kay will probably say this
is inefficient, but at least it is fairly clear and simple.  I leave the
real formatting to you (that is, I'm still using <br/> elements for quick
and dirty newlines  in an IE display).

Notice that the expression from the original version, that retrieved that
name of the parent category of a topic, has become part of the restriction
on the allowed topic nodes.  It's still the same path expression, though,
since it is still referring to the same elements, the parent categories.

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

<xsl:variable name='allowed-elements'
   select='/doc/category/topic[count(preceding::topic)&lt;4]'/>

<xsl:template match="/">
<results>
     <xsl:apply-templates select='doc/category'/>
</results>
</xsl:template>

<xsl:template match='category'>
 <xsl:variable name='category' select='@value'/>
 <xsl:value-of select='$category'/><br/>
 <xsl:apply-templates
      select='$allowed-elements[ancestor::category[1]/
            @value=$category]'/>
</xsl:template>

<xsl:template match='topic'>
        <xsl:value-of select='@value'/><br/>
</xsl:template>


</xsl:stylesheet>
==============================

Results:
===============================
<results>
  category1
  <br />
  topic1
  <br />
  topic2
  <br />
  topic3
  <br />
  category2
  <br />
  topic1
  <br />
  </results>
===========================

Cheers,

Tom P


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


Current Thread