xsl-list
[Top] [All Lists]

RE: following-sibling problem

2004-10-27 06:38:05
This looks like the classical "positional grouping" problem in its recursive
form.

For a very similar problem I used:

<xsl:template name="process-level">
  <xsl:param name="population" required="yes" as="element()*"/>
  <xsl:param name="level" required="yes" as="xs:integer"/>
  <xsl:for-each-group select="$population" 
                      group-starting-with="*[xs:integer(@level) eq $level]">
    <xsl:element name="{(_at_)tag}">
      <xsl:copy-of select="@ID[string(.)], @REF[string(.)]"/>
      <xsl:value-of select="normalize-space(@text)"/>
      <xsl:call-template name="process-level">
        <xsl:with-param name="population" select="current-group() except
."/>
        <xsl:with-param name="level" select="$level + 1"/>
      </xsl:call-template>
    </xsl:element>
  </xsl:for-each-group>
</xsl:template>

Prime this with the population parameter set to the entire sequence of key
elements, and level set to 0, and it will build the hierarchy for you; you
only need to change the instructions that construct new elements and
attributes in the new hierarchy.

Michael Kay
http://www.saxonica.com/

-----Original Message-----
From: Bruce D'Arcus [mailto:bdarcus(_at_)myrealbox(_dot_)com] 
Sent: 27 October 2004 13:56
To: XSL-List(_at_)lists(_dot_)mulberrytech(_dot_)com
Subject: [xsl] following-sibling problem

This is driving me crazy.

Source:

                      <bullets>
                              <bullet level="0" 
marker-type="inherited">
                                      <content>Heading</content>
                              </bullet>
                              <bullet level="1" 
marker-type="inherited">
                                      <content>Bullet 1a</content>
                              </bullet>
                              <bullet level="1" 
marker-type="inherited">
                                      <content>Bullet 1b</content>
                              </bullet>
                              <bullet level="2" 
marker-type="inherited">
                                      <content>Bullet 2a - "a 
quote''</content>
                              </bullet>
                              <bullet level="2" 
marker-type="inherited" id="bullet-1">
                                      <content>Bullet 2b</content>
                              </bullet>
                      </bullets>

Templates:

<xsl:template match="key:bullet[(_at_)level='0']" mode="normal">
   <h2><xsl:value-of select="."/></h2>
   <ul>
     <xsl:apply-templates 
select="following-sibling::key:bullet[(_at_)level='1']"/>
   </ul>
</xsl:template>

<xsl:template match="key:bullet[(_at_)level='1']">
   <li class="level1">
     <xsl:value-of select="."/>
     <xsl:if test="following-sibling::key:bullet[(_at_)level='2']">
       <ul>
         <xsl:apply-templates 
select="following-sibling::key:bullet[(_at_)level='2']"/>
       </ul>
     </xsl:if>
   </li>
</xsl:template>

<xsl:template match="key:bullet[(_at_)level='2']">
   <li class="level2">
     <xsl:value-of select="."/>
   </li>
</xsl:template>


Output:

         <h2>Heading</h2>
         <ul>
           <li class="level1">Bullet 1a<ul>
               <li class="level2">Bullet 2a - "a quote''</li>
               <li class="level2">Bullet 2b</li>
             </ul>
           </li>
           <li class="level1">Bullet 1b<ul>
               <li class="level2">Bullet 2a - "a quote''</li>
               <li class="level2">Bullet 2b</li>
             </ul>
           </li>
         </ul>

What I want:

         <h2>Heading</h2>
         <ul>
           <li class="level1">Bullet 1a</li>
           <li class="level1">Bullet 1b<ul>
               <li class="level2">Bullet 2a - "a quote''</li>
               <li class="level2">Bullet 2b</li>
             </ul>
           </li>
         </ul>

In other words, I only want the template for level 2 applied 
when there 
is a following-sibling immediately following a level 1 bullet.

Bruce

--+------------------------------------------------------------------
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>
--+--





<Prev in Thread] Current Thread [Next in Thread>