[xsl] Re: Finding position of a node relative to the root instead of the parent node

Subject: [xsl] Re: Finding position of a node relative to the root instead of the parent node
From: "Dimitre Novatchev" <dnovatchev@xxxxxxxxx>
Date: Tue, 23 Dec 2003 11:02:01 +0100
Your problem is that in your code you have expressions like this:

    //movie[position() &lt;=  number($thisMany)]

When actually you want this expression instead:

    (//movie)[position() &lt;=  number($thisMany)]


Investigate this problem (e.g. look into the XSL FAQ) and try to understand
the difference between the two XPath expressions. This is a FAQ about using
the abbreviation //

So, you have to make just two changes in your code:

1. Change:

>       <xsl:for-each select="//movie[position() &lt;=
> number($thisMany)]">


to:

     <xsl:for-each select="(//movie) [position() &lt;= number($thisMany)]">


2. Change:

>       <xsl:for-each select="//*[name()='movie'][position() &gt;
> number($thisMany)]">

to:

     <xsl:for-each select="(//*[name()='movie']) [position() &gt;
number($thisMany)]">



The corrected code now produces a nice 2-column table.



Dimitre Novatchev.
FXSL developer

http://fxsl.sourceforge.net/ -- the home of FXSL
Resume: http://fxsl.sf.net/DNovatchev/Resume/Res.html



"Cynthia DeLaria" <cdelaria@xxxxxxxxx> wrote in message
news:CE54CE22EF3A65438885197A240E408BC5772D@xxxxxxxxxxxxxxxxxxx
> Good Day,
>
> I have searched the xsl list unsuccessfully for an answer to this
> question, although I'm sure something like this has been addressed. I
> think I'm just not sure what to search on to find it.
>
> I have the following xml snippet:
>
> <nr>
>     <featured>
>         <movie title="Charlie's Angles: Full
> Throttle">Description</movie>
>         <movie title="28 Days Later">Description</movie>
>         <movie title="The Santa Clause 2">Description</movie>
>     </featured>
>     <also_new>
>         <movie title="Northfork" />
>         <movie title="Rudy: The Rudy Giuliani Story" />
>         <movie title="Russian Ark" />
>     </also_new>
> </nr>
>
> Basically, in the fully-flushed out version of the XML, the <featured>
> movies have images and full descriptions, while the <also_new> movies
> have only a title and rating. What I need to do is find a way to find
> the position of each <movie> node relative to the root, as I need to
> create a "print the new releases" page that lists all new releases in
> two columns. This is what I tried, but it gives me the position based on
> the parent (i.e. <also_new> or <featured>) not relative to the root.
>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
> <xsl:output method="html" encoding="ISO-8859-1" />
> <xsl:template match="/nr">
> <html>
> <head>
> <title>New Releases E-Newsletter</title>
> <head>
> <body bgcolor="#ffffff" text="#000000" link="#023f7e" alink="#ff0000"
> vlink="#023f7e">
> Just print out this list.<br />
>     <table width="100%">
>     <xsl:variable name="thisMany"><xsl:value-of
> select="(count(//*[name()='movie']) div 2)" /></xsl:variable>
>      <tr>
>       <td valign="top" width="50%" class="body2">
>       <br />
>       <xsl:for-each select="//movie[position() &lt;=
> number($thisMany)]">
>       <b><i><xsl:value-of select="./@title" /></i></b><br />
>       </xsl:for-each>
>       <br />
>       </td>
>       <td valign="top" width="50%" class="body2">
>       <br />
>       <xsl:for-each select="//*[name()='movie'][position() &gt;
> number($thisMany)]">
>       <b><i><xsl:value-of select="./@title" /></i></b><br />
>       </xsl:for-each>
>       <br />
>       </td>
>      </tr>
>     </table>
>  </body>
> </html>
> </xsl:template>
> </xsl:stylesheet>
>
> Is it possible to get the position relative to the root node? It seems
> like this should be very simple, but all of the things I have tried have
> not worked to produce the intended outcome.
>
> Thank you!
>
> Cynthia
>
>
>  XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
>
>




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


Current Thread