RE: [xsl] Muenchian method, and keys 'n stuff

Subject: RE: [xsl] Muenchian method, and keys 'n stuff
From: DPawson@xxxxxxxxxxx
Date: Tue, 22 Jan 2002 16:34:58 -0000
Still having fun:
Jeni's solution [1] is the way I'm trying to go.
>From the solution below, I've refined it to:

 <xsl:template match="adventure">
    <h1>Adventure</h1>

    <xsl:call-template name="alphabetical">
      <xsl:with-param name="section" select="generate-id()"/>
    </xsl:call-template>
  </xsl:template>
so it passes the id val to the template. (not being used in this instance)


the problem is now that the template below never
fires the otherwise clause of the choose, and I can't see why.

I've limited the key to match on adventure/r to reduce the problem size.
<xsl:key name="auth" 
  match="adventure/r"
   use="Ra/lett"/>
so the scope should now be restricted to those r elements 
which are children of adventure.





<xsl:template name="alphabetical">
  <xsl:param name="section" select="'none'"/>
  <xsl:param name="alphabet" select="'abcdefghijklmnopqrstuvwxyz'" />
  <xsl:if test="$alphabet != ''">
    <xsl:variable name="letter"
                  select="substring($alphabet, 1, 1)" />

    <xsl:variable name="auths"
      select="key('auth', $letter )" />
      <xsl:if test="$letter ='e'">
        <xsl:message> Letter e:: <xsl:value-of select="$auths"/>
</xsl:message>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="$auths/node()">
        <xsl:apply-templates select="r[generate-id(.)=
                      generate-id(key('auth',Ra/lett)[1])]"/>
      </xsl:when>
      <xsl:otherwise>
        <p>
          There are no authors beginning with
          <xsl:value-of select="$letter" />
        </p>
      </xsl:otherwise>
    </xsl:choose>
    
    <xsl:call-template name="alphabetical">
      <xsl:with-param name="alphabet"
                      select="substring($alphabet, 2)" />
        <xsl:with-param name="section" select="$section"/>
    </xsl:call-template>
  </xsl:if>
</xsl:template>


Regards DaveP


> I'd just iterate over the alphabet held in a string, with a recursive
> template. It starts off with the whole alphabet and gradually whittles
> it down letter by letter until there's no alphabet left:
> 
> <xsl:template name="alphabetical">
>   <xsl:param name="alphabet" select="'abcdefghijklmnopqrstuvwxyz'" />
>   <xsl:if test="$alphabet != ''">
>     <xsl:variable name="letter"
>                   select="substring($alphabet, 1, 1)" />
> 
>     ... do things with the letter ...
>     
>     <xsl:call-template name="alphabetical">
>       <xsl:with-param name="alphabet"
>                       select="substring($alphabet, 2)" />
>     </xsl:call-template>
>   </xsl:if>
> </xsl:template>
> 
> In your case, the thing you want to do with the letter is identify
> those r elements whose Ra/lett is that letter. You can do this with
> the key that you've made, as follows:
> 
> <xsl:template name="alphabetical">
>   <xsl:param name="alphabet" select="'abcdefghijklmnopqrstuvwxyz'" />
>   <xsl:if test="$alphabet != ''">
>     <xsl:variable name="letter"
>                   select="substring($alphabet, 1, 1)" />
> 
>     <xsl:variable name="auths"
>                   select="key('auth', $letter)" />
>     <xsl:choose>
>       <xsl:when test="$auths">
>         <h2>
>           The letter
>           <xsl:value-of select="translate($letter, $l, $u)" />
>         </h2>
>         <xsl:for-each select="$auths">
>           <h3><xsl:value-of select="Ra/au" /></h3>
>         </xsl:for-each>
>       </xsl:when>
>       <xsl:otherwise>
>         <p>
>           There are no authors beginning with
>           <xsl:value-of select="$letter" />
>         </p>
>       </xsl:otherwise>
>     </xsl:choose>
>     
>     <xsl:call-template name="alphabetical">
>       <xsl:with-param name="alphabet"
>                       select="substring($alphabet, 2)" />
>     </xsl:call-template>
>   </xsl:if>
> </xsl:template>
> 
> To call the template, all you need is:
> 
>   <xsl:call-template name="alphabetical" />
> 
> Note that the key will give you all r elements with the same Ra/lett,
> and as such it won't be scoped within the 'adventure'. To get the
> scoping, you need to do one of the following:
> 
[1]
>   - include the adventure's (generated) ID within the key, and pass
>     that ID to the alphabetical template
>   - don't use the key to retrieve the rs with a particular Ra/lett,
>     just use r[Ra/lett = $letter] instead (less efficient than a key,
>     probably)
> 
> Cheers,
> 
> Jeni


- 

NOTICE: The information contained in this email and any attachments is 
confidential and may be legally privileged. If you are not the 
intended recipient you are hereby notified that you must not use, 
disclose, distribute, copy, print or rely on this email's content. If 
you are not the intended recipient, please notify the sender 
immediately and then delete the email and any attachments from your 
system.

RNIB has made strenuous efforts to ensure that emails and any 
attachments generated by its staff are free from viruses. However, it 
cannot accept any responsibility for any viruses which are 
transmitted. We therefore recommend you scan all attachments.

Please note that the statements and views expressed in this email 
and any attachments are those of the author and do not necessarily 
represent those of RNIB.

RNIB Registered Charity Number: 226227

Website: http://www.rnib.org.uk 


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


Current Thread