|
[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: counting preceding identical elements
Steven,
It's not that position() is "bad practice" -- actually, it can be very good practice. The reason it's dangerous is because understanding the way it works depends on a good grasp of how parameters like the context node and the current node set are determined by the processor. If you don't have this deeper grasp of XPath, you'll be left guessing how position() works, and you'll usually guess wrong. Yours is a perfect case in point. For example, count(//section[position()=$section]/preceding::sibling[position()=$slide]) won't give you anything useful, although it's a correctly formed XPath location path. Specifically, the step preceding::sibling[position()=$slide] looks for all elements named 'sibling' before the context node (of the step, which will be a 'section' element), taking only those whose position *among the preceding elements named 'sibling', counting in reverse document order away from the section* (since the preceding axis is a 'reverse' axis), is equal to $slide. That isn't what you want at all. Typing "preceding::sibling" when you probably meant "preceding-sibling::slide" is part of the problem (not that that's what you want, since I don't think your sections have slides as preceding siblings, and even if they did, that's not what you want to count). But the way position() works is also getting you into trouble. Now, to how to solve the problem. You say What I basically try to do is to calculate the number of preceding slide elements of the slide element which position()=$slide and of which the parent section element's position()=$section. As you seem to have surmised, this is much easier to do if your slide is the context node, than when it is not. The solution is to go ahead and change contexts, for this operation. Changing contexts can be done with an xsl:for-each. For example: <xsl:variable name="absolute-slide-number"> <xsl:for-each select="//section[position()=$section]/slide[position()=$slide]"> <xsl:value-of select="count(preceding::slide)+1"/> </xsl:for-each> </xsl:variable> This will give you the count of all slides preceding your target slide, in all sections. Is that what you wanted? <xsl:number> is also another way to go, but it too will require you to change your current node. Your efforts in that direction haven't worked because you apparently don't know how the 'count' attribute works there (it doesn't specify the node whose position you want, but the type of nodes you want to count, using a match pattern). Try <xsl:number count="slide" level="any"/>. If I'm guessing wrong about what you need, please post again with a clarification of your requirements (maybe a fuller code sample). Good luck, Wendell At 06:21 PM 8/24/01, you wrote: Hi, ====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ====================================================================== XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list
|
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
|

Cart








