[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: Preceding siblings test
On 9/14/2010 10:23 AM, Martin Honnen wrote: > Hintz, David wrote: > >> I'm still a novice at XSLT and am having a problem coming up with a >> test for elements preceding a particular element, but only up to the >> first occurrence of the same (or parent) element. For example, in >> this XML: >> >> <list> >> <a/> >> <item>...</item> >> <b/><b/><a/> >> <item>...</item> >> <item>...</item> >> <a/><a/> >> <item>...</item> >> </list> >> >> Assume in this example, <a> and <b> elements can occur before each >> item (any number of each). When I start processing an <item>, how do >> I get a list of just those nodes before the first preceding <item> or >> parent <list> element? > > With XSLT 2.0 you can use the XPath 2.0 '>>' operator > <xsl:template match="item"> > <xsl:variable name="preceding-item" > select="preceding-sibling::item[1]"/> > <xsl:variable name="preceding-abs" select="preceding-sibling:a[. > >> $preceding-item] | preceding-sibling:b[. >> > $preceding-item]"/> > </xsl:template> > Whoa! The things I didn't know I didn't know about XSLT 2.0... like this >> operator! Thanks Martin. A few notes for David about the above code... 1) It isn't necessary to escape '>'. So you can make it more readable. 2) The XPath for preceding-abs has a typo: single colons instead of double, for the preceding-sibling axis. 3) You can make the XPath expression a little shorter and less redundant by factoring out the p-s axis and the predicate, combining a and b via "[self::a or self::b]". Thus: <xsl:variable name="preceding-abs" select="preceding-sibling::*[self::a or self::b][. >> $preceding-item]"/> Lars
|
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
|