[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] RE: Elements just before/after the current one
Hi Nicolas, When you are working with preceding-sibling you are working from the perspective of a reverse axis so preceding-sibling::*[1] goes backwards 1 position, preceding-sibling::*[2] goes back 2 and so forth... Some quick XSLT to showcase this is... XML: <?xml version="1.0"?> <foo> <bar>1</bar> <bar>2</bar> <bar>3</bar> <bar>4</bar> <bar>5</bar> <bar>6</bar> <bar>7</bar> <bar>8</bar> </foo> XSLT: <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <xsl:apply-templates select="foo/bar[8]"/> </xsl:template> <xsl:template match="bar"> <xsl:value-of select="preceding-sibling::bar[3]"/> </xsl:template> </xsl:stylesheet> Produces and output of: 5 Hope this helps! <M:D/> > -----Original Message----- > From: Nicolas Pillot [mailto:nicolas.pillot@xxxxxxxxxx] > Sent: Thursday, May 27, 2004 12:40 PM > To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx > Subject: Elements just before/after the current one > > > Hello! > > I have a problem, as you can guess. I am struggling to find the element > that is just before, and the element just after the current one. > > What i am doing now for the element after the current one is : > following-sibling::*[position() = 1] > And this seems to work. But for the element just before, i tried > following-sibling::*[last()] > but this gives me the first (topmost) item instead of the last ! > So i debugged with : > > <xsl:variable name="prevcount"> > <xsl:value-of select="count(preceding-sibling::*)" /> > </xsl:variable> > <xsl:for-each select="preceding-sibling::*"> > <xsl:element name="before"> > <xsl:value-of select="."/> <!-- or name() --> > <xsl:value-of select="position()"/> > <xsl:if test="position() = $prevcount"> > selected > </xsl:if> > </xsl:element> > </xsl:for-each> > > And the "selected" text appears right next to the good element. > Then i tried the following to sum it up : > > <xsl:variable name="prevcount"> > <xsl:value-of select="count(preceding-sibling::*)" /> > </xsl:variable> > <xsl:value-of select="preceding-sibling::*[position() = $prevcount]" > /> > > But again, it give me the content of the first (topmost) node !! > What am i doing wrong ?! Is there a simpler way to get these "special" > items ? > > Any help would be greatly appreciated ! > Nicolas
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|