Re: [xsl]Problem with Position()?

Subject: Re: [xsl]Problem with Position()?
From: "Jay Bryant" <jay@xxxxxxxxxxxx>
Date: Sat, 13 Oct 2007 11:01:29 -0500
Hi,

I am trying to compile a document where I can display things differently based on the position of the <p> tags. However, now it does not print back anything that is in the <p> in the XSLT below? Can someone please tell me what is wrong with this?

XSLT:

<xsl:template match="book">

<div>
   <xsl:apply-templates select="./@author"/>,
   <i><xsl:apply-templates select="./@title"/></i>.
    (<xsl:apply-templates select="./@topic"/>)
    [  <xsl:apply-templates/>]
            </div>
   </xsl:template>

<xsl:template match="p">
       <xsl:choose>
           <xsl:when test="position()!=1">
               <p>
                   <xsl:call-template name="rend"/>
                   <xsl:apply-templates/>
               </p>
           </xsl:when>
           <xsl:otherwise>
               <xsl:if test="position()=1">
                   <xsl:apply-templates/>
               </xsl:if>
           </xsl:otherwise>
       </xsl:choose>
   </xsl:template>

XML:

<book topic="Technology" author="Tennison, Jeni" title="Beginning XSLT">

<p>New York: Apress, 2002.</p>
<p>The book serves as an introduction to XSLT based on the examples of a television program set. A resourceful book for those new to the technology.</p>
</book>


Intended Output:

Tennison, Jeni, Beginning XSLT. (Technology). New York: Apress. 2002
The book serves as an introduction to XSLT based on the examples of a television program set. A resourceful book for those new to the technology.


Thanks to those who can help.

Alice

Hi, Alice,


I suspect your problem is because position() never equals 1 for those p elements. I added some test lines to your templates to discover the values the position() function returned for the p elements and got 2 and 4. Testing to see if the value of position() = 1 won't work.

Try changing the test to use the preceding-sibling axis, thus:

<xsl:when test="count(preceding-sibling::p) = 0">

That test catches the first p in a series of neighboring p elements.

HTH

Jay Bryant
Bryant Communication Services
http://www.bryantcs.com/


Current Thread