[xsl] Finding position of specific element node in nodeset context

Subject: [xsl] Finding position of specific element node in nodeset context
From: "Sean Healy" <shealy@xxxxxxxxxxxxx>
Date: Sat, 24 May 2003 23:30:43 -0600
How do I construct a variable such that it represents the position of a specific element node? Thatis, I‘d like to build an XPath expression that finds the relative position of the first “ChapterNumber” that the parser encounters when iterating over the last “frontBreak”.  
         
For example, here’s the XML:

<?xml version="1.0"?>
<book>
<section>
<p stylename="docTitle" <string >Lucky</string></p>
<p stylename="FrontMatter" <string>BY </string></p>
…
<p stylename="frontBreak" align="right" fontsize="24"></p>
<p stylename="docAuthor" <string> Joe Smith</string></p>
<p stylename="docTitle" <string >Lucky</string></p>
<p stylename="FrontMatter" <string>BY </string></p>
<--Here’s frontBreak[last()]-->
<p stylename="frontBreak" align="right" fontsize="24"></p>
<p stylename="docAuthor" <string> Joe Smith </string></p>
<p stylename="FrontMatter" <string>BY </string></p>
<!--Here’s ChapterNumber[1]-->
<p stylename="ChapterNumber" <string>1</string></p>
<p stylename=" somethingelse3" <string> text3</string></p>
<p stylename="ChapterNumber" <string>1</string></p>
<p stylename="somethingelse1" <string >text1</string></p>
<p stylename=" somethingelse3" <string> text3</string></p>
…
</section>
</book>

Here’s what my template looks like so far:

<xsl:template match="p[@stylename='frontBreak']">
<!—Note that I’ve hardcoded $chaptnum_limit, but I would like an Xpath expression to replace ‘3’.-->
<xsl:variable name="chaptnum_limit" select="3"/>
    <xsl:element name="div1">
         <xsl:attribute name="n"><xsl:value-of select="position()"/></xsl:attribute> 
         <xsl:attribute name="type">front</xsl:attribute> 
             <xsl:element name="head"><xsl:value-of select="node()"/>
    </xsl:element>
   <xsl:for-each select=".">
      <xsl:variable name="frt_br" select="generate-id()"/>
         <xsl:for-each select="following-sibling::p[@stylename != 'frontBreak']
            [generate-id(preceding-sibling::p[@stylename = 'frontBreak'][1]) = $frt_br]">
                 
                <xsl:if test="position() &lt; $chaptnum_limit">
                      <xsl:value-of select="."/>
                </xsl:if>
           
       </xsl:for-each>
   </xsl:for-each>
   </xsl:element>
 </xsl:template>

I am not sure if this is the right approach (<xsl:if test="position() &lt; $chaptnum_limit">), but it was all I could come up with to keep this template from providing the values of all <p>’s until EOF when processing frontBreak[last()]. Thank you for your help in advance. –shealy


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


Current Thread