[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Table of contents - for-each question


Subject: [xsl] Table of contents - for-each question
From: llobash@xxxxxxxx
Date: Tue, 24 Jan 2006 11:34:06 -0500

I am attempting to create a table of contents to include all elements with
<head> tag + all (c01) elements with "series" attribute and (c02 or c03)
elements with "subseries" attributes. c0x tags from Encoded Archival
Description standards, probably not important. Table of contents does
require indentation based on level in heirarchy.
The for-each statement - in bold below - is not doing what I expected. It
is selecting all elements with <head> but only the first c02 to follow each
c01 in the document.

See entire TOC section below + resulting html:

Thanks in advance for any help.
Lynn

<!-- entering toc mode here. match on archdesc. create div, bold font add
text "table of contents" call toc template. plus formatting for hard rule,
break, align. -->

<xsl:template mode='toc' match='archdesc'>
      <div id="toc">
            <div style="font-weight:bold">
                  <xsl:text>Table of Contents</xsl:text>
            </div>
            <xsl:call-template name='toc'/>
      </div>
      <br/><hr width="30%" style="text-align:center"/><br/>
</xsl:template>

<!-- toc template testing to determine level.
parameter htype = head type.
parameter name levels 3-1 used to determine level.
parameter name hindent used to determine indentation. -->
<!-- when test - level = 3 set text to no indent -->

<xsl:template name='toc'>
      <xsl:param name='levels'>3</xsl:param>
      <xsl:param name='htype'>
            <xsl:choose>
                  <xsl:when test='$levels="3"'>
                        <xsl:text>div</xsl:text>
                  </xsl:when>

<!-- if test result = no -->

                  <xsl:otherwise>
                        <xsl:text>div</xsl:text>
                  </xsl:otherwise>
            </xsl:choose>
      </xsl:param>

<!-- if test level = 2 indent text 12  -->

      <xsl:param name='hindent'>
            <xsl:choose>
                  <xsl:when test='$levels="2"'>
                        <xsl:text>12</xsl:text>
                  </xsl:when>


<!-- if test level = 1 indent text 24 -->
                        <xsl:when test='$levels="1"'>
                              <xsl:text>24</xsl:text>
                        </xsl:when>



                        <xsl:otherwise>
                              <xsl:text>0</xsl:text>
                        </xsl:otherwise>
                  </xsl:choose>
      </xsl:param>

<!-- this line dumps you out of loop. if level equal 0. -->
      <xsl:if test='$levels != 0'>


<!-- for each checks each node in doc order. follows tree from top down to
last child
back to top from left to right.  -->
<!-- for each node, select any element with a head,
not controlaccess which is hidden and should not appeaer in TOC. -->

<xsl:for-each select='* [head]| * [@level="series"]| *
[@level="subseries"]'>


<!-- calling htype, head type created above. use {} when you want to
invoke a function. unless dealing with xsl element with select. do not use
{}.
has to do with when it is expecting text and not.-->


                  <xsl:element name='{$htype}'>


<!-- attribute style for htype. indentation and size for each htype output
here. -->

                        <xsl:attribute name='style'>
                        <xsl:text>text-indent:</xsl:text>

                        <xsl:value-of select='$hindent'/>
                        <xsl:text>pt;</xsl:text>
                        </xsl:attribute>

<!-- adding href anchor to each id on head element. -->

                        <a href="#{generate-id()}">
                              <xsl:value-of select='head | c01
[@level="series"]/did/unittitle | c02 [@level="subseries"]/did/unittitle |
c03         [@level="subseries"]/did/unittitle'/>
                        </a>


                  </xsl:element><!-- end: {$htype} -->

<!-- invoking toc. toc starts at 3 because we know in advance that we have
four levels.
it loops through 4 times 3-0 so that we dump out at end. -->

                  <xsl:call-template name='toc'>
                        <xsl:with-param name='levels'>
                              <xsl:value-of select='$levels - 1'/>
                        </xsl:with-param>
                  </xsl:call-template>
            </xsl:for-each>
      </xsl:if>
</xsl:template>

<!-- End TOC mode here. -->

OUTPUT

<br><hr width="30%" style="text-align:center"><br><div id="toc">
         <div style="font-weight:bold">Table of Contents</div>
         <div style="text-indent:0pt;"><a href="#d0e86">Descriptive
Summary</a></div>
         <div style="text-indent:0pt;"><a href="#d0e116">Administrative
Information</a></div>
         <div style="text-indent:0pt;"><a href="#d0e131">Index
Terms</a></div>
         <div style="text-indent:12pt;"><a href="#d0e134">Names</a></div>
         <div style="text-indent:12pt;"><a
href="#d0e171">Organizations</a></div>
         <div style="text-indent:12pt;"><a
href="#d0e182">Subjects</a></div>
         <div style="text-indent:12pt;"><a href="#d0e195">Document
Types</a></div>
         <div style="text-indent:0pt;"><a href="#d0e216">Biographical
note</a></div>
         <div style="text-indent:0pt;"><a href="#d0e249">Scope and Content
Note</a></div>
         <div style="text-indent:0pt;"><a
href="#d0e260">Organization</a></div>
         <div style="text-indent:0pt;"><a href="#d0e280">Related
Collections</a></div>
         <div style="text-indent:0pt;"><a href="#d0e299">Series
Descriptions and Container List</a></div>
         <div style="text-indent:12pt;"><a href="#d0e302">A. General
                                           correspondence,
                                           1895-1949 and undated</a></div>
         <div style="text-indent:24pt;"><a href="#d0e313"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e606"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1103"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e1125">A. Diaries,
                                           1894-1933 and undated</a></div>
         <div style="text-indent:24pt;"><a href="#d0e1133"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1230"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e1273">A. General,
                                           1907-1944 and undated</a></div>
         <div style="text-indent:24pt;"><a href="#d0e1281"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1312"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1366"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e1395">A. Legal,
                                           1909-1943 and undated</a></div>
         <div style="text-indent:24pt;"><a href="#d0e1403"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1476"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e1624">A. School and
                                           family mementos,
                                           1886-1944 and undated</a></div>
         <div style="text-indent:24pt;"><a href="#d0e1635"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1818"></a></div>
         <div style="text-indent:24pt;"><a href="#d0e1899"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e1973"></a></div>
         <div style="text-indent:12pt;"><a href="#d0e2249"></a></div>


Current Thread
Keywords
xsl