This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: Counting nodes processed


All,

thanks for the help yesterday.  However, as I was working on this problem I realized that my xml description was slightly different.  There's a new twist.  The xml description is now similar to the following:

<Root>
  <TopicOwner id="1" name="owner1">
     <Category id ="1" name="Sports">
        <Topic id="1" name="Basketball"/>
        <Topic id="2" name="Football"/>
        <Topic id="3" name="Baseball"/>
     </Category>
     <Category id="2" name="FooCategory">
        <Topic id="4" name="FooTopic"/>
     </Category>
  </TopicOwner>
  <TopicOwner id="2" name="owner2">
    <Category id="2" name="Entertainment">
        <Topic id="4" name="Puzzles"/>
        <Topic id="5" name="Games"/>
    </Category>
  </TopicOwner>
</Root>

Each owner may have > 1 category and each category has > 1 topic.  The problem is still the same.  I want to retrieve only X number of topics for each owner.  So, assuming I want to retrieve 4 topics per owner, the output should be

Owner1
  Sports
     Basketball
     Football
     Baseball
  FooCategory
     FooTopic

Owner2
  Entertainment
     Puzzles
     Games

Thanks,
jay

-----Original Message-----
From: Thomas B. Passin [mailto:tpassin@mitretek.org]
Sent: Monday, March 04, 2002 3:18 PM
To: xsl-list@lists.mulberrytech.com
Subject: Re: [xsl] Counting nodes processed


[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

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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]