Altova Mailing List Archives>Archive Index >microsoft.public.xsl Archive Home >Recent entries >Thread Prev - Rendering problem [Thread Next] Re: Rendering problemTo: NULL Date: 6/1/2005 10:05:00 AM Roshawn Dawson wrote: > Hi, > > I have an xml file that contains a list of books. Each book can have > one or more authors. What I'd like to do place each author in a > separate span tag. All authors are to have commas behind them except > for the last author. Furthermore, the last author is to be preceded by > the word "and." Here's my code so far: > > xml doc > <books> > <book> > <title>Some Title Here</title> > <author>Author 1</author> > </book> > <book> > <title>Another Title</title> > <author>Author Here</author> > <author>Author There</author> > <author>Author Everywhere</author> > </book> > <book> > <title>One More Title</title> > <author>Lex Luthor</author> > <author>Grog</author> > <author>Braniac</author> > </book> > </books> > > xsl code > <xsl:template match="books"> > <xsl:for-each select="book"> > <h2><xsl:value-of select="title"/></h2> > <span class="authors"><xsl:value-of select="Author"/></span> > </xsl:for-each> > </xsl:template> > > I have no idea where to begin accomplishing my goal. Any ideas? > > Thanks, > Roshawn Roshawn, For-Each's are usually unnecessary in XSL, as apply templates does basically the same thing. Using templates will match all the nodes within the context node that conform both to the select attribute in apply-templates and the match attribute in template. Code is also more easily written and read using templates instead of for-each, IMHO. One of the instances that I do find for-each's useful is when I need to perform more than one transform to the same node set within a stylesheet. The stylesheet below will accomplish what you are trying to do. XSL: ---- <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="html" version="4.0" indent="yes"/> <xsl:template match="/books"> <xsl:apply-templates select="book" /> </xsl:template> <xsl:template match="book"> <h2><xsl:value-of select="title" /></h2> <xsl:apply-templates select="author" /> </xsl:template> <xsl:template match="author"> <xsl:if test="position()=last() and position()!=1">and </xsl:if> <span class="authors"><xsl:value-of select="." /></span> <xsl:if test="position()!=last()">, </xsl:if> </xsl:template> </xsl:stylesheet> Regards, N. Demos -- Change "seven" to a digit to email me. | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
