[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: simple XPath question
Hi Chris, > And the position() values are really a separate thing entirely. They > are positions in the source tree, not in the node-set, since nodes > in node-sets don't have positions. > > (Do I have this right?) Almost. You're right that nodes in node sets don't have positions (since node sets are by definition unordered), but nodes *can* have positions in the context node list. How position() works depends on whether you are using it in a *pattern* (in a match attribute) or in an expression (in a select or test attribute). When you *select* or *test* nodes with an expression, then position() gives the position of the context node in the context node list. If you select nodes to apply templates to: <xsl:template match="warehouse"> <xsl:apply-templates select="item/country" /> </xsl:template> then within the template for those nodes, the current node list contains all the country children of the item children of the warehouse element. The current node is the country element that you're looking at in the current node list. So if you do: <xsl:template match="country"> <xsl:value-of select="position()" />: <xsl:value-of select="." /> </xsl:template> Then you will get a sequentially numbered list of countries. When you *match* nodes with a pattern, then position() gives the position of the node amongst its similar siblings - it always uses the source tree to work out the position because match patterns have no concept of context node lists. So as you've seen, if you do: <xsl:template match="country[1]"> ... </xsl:template> then this template will match any country element that is the first country element child of its parent. Personally, I try to avoid using position() in templates because it's easy to lose track of what the current node list holds; if I need to use position() then I usually use xsl:for-each to iterate over the nodes rather than applying templates to them, so: <xsl:template match="warehouse"> <xsl:for-each select="item/country"> <xsl:choose> <xsl:when test="position() = 1"> <first-country> <xsl:value-of select="." /> </first-country> </xsl:when> <xsl:otherwise> <country> <xsl:value-of select="." /> </country> </xsl:otherwise> </xsl:choose> </xsl:for-each> </xsl:template> Cheers, Jeni --- Jeni Tennison http://www.jenitennison.com/ 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
|