[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message] Re: alternate row color in a table
Hi Sébastien, > ***XML*** > <?xml version="1.0" encoding="UTF-8"?> > <?xml:stylesheet type="text/xsl" href="simple4.xsl"?> > <data> > <info>aaaaa</info> > <info>bbbbb</info> > <grinfo><info>ccccc</info></grinfo> > <grinfo><subinfo>blabla</subinfo><info>ddddd</info></grinfo> > </data> [snip] > the row with ccccc and the row with ddddd have the same bgcolor > using position() in this case seems to work just with siblings. Actually, the position() function works on the list of nodes that you *select* to process, and that can make it look as though it just works on siblings. In your template for the data element: <xsl:template match="data"> <table><xsl:apply-templates/></table> </xsl:template> to get the content of the table, you apply templates to all the child nodes of the data element. There are actually nine child nodes for the data element in your example: two info elements, two grinfo elements, and between the elements five whitespace-only text nodes (each containing a line break so that the elements start on different lines). The positions of these nine nodes are 1, 2, 3, ..., 8, 9. Because of the whitespace, the two info elements have the position 2 and 4. The other info elements that you're interested in are within the grinfo elements. The template for the grinfo elements is simply: <xsl:template match="grinfo"> <xsl:apply-templates/> </xsl:template> Fortunately, there's no whitespace to worry about, but the other subinfo element in the second grinfo element does make a difference. So the info element in the first grinfo element has a position of 1. And the info element in the second grinfo element has a position of 2. So the positions you end up with for the info elements are 2, 4, 1, 2. To get the info elements to have the right positions (and therefore to get your alternate colouring to work), you need to process the info elements such that their positions are 1, 2, 3, and 4. In other words, you need to process a node set that just contains the four info elements. You can get hold of the four info elements that you're interested in from the template matching the data element with the location path: .//info (Or you could use info | grinfo/info, but I'm guessing that your real source data is slightly more complicated than your sample, and the info elements might appear at any level.) The template for the data element, then, should look like: <xsl:template match="data"> <table><xsl:apply-templates select=".//info" /></table> </xsl:template> And you can get rid of the template matching grinfo elements since it will never be used. Making that change should make the template for the info elements work. 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
|