Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - Subject: How can I use XSLT / Xpath to sort elements for display and treat the last one displayed differently? >Thread Next - Re: Subject: How can I use XSLT / Xpath to sort elements for display and treat the last one displayed differently? Re: Subject: How can I use XSLT / Xpath to sort elements for display and treat the last one displayed differently?To: NULL Date: 1/8/2008 11:13:00 AM "admyc" <admyc@h...> wrote in message news:28ba3454-55af-49c1-8dd3-32f6263b5cbd@4...... > Hello > > I am trying to use XSLT to display an XML document on a web-browser. > > The XML document has products that contain ingredients: > <product> > <ingredients> > <ingredient mass-order="2">apple juice</ingredient> > <ingredient mass-order=1"">orange juice</ingredient> > <ingredient mass-order="3">banana</ingredient> > </ingredients> > </product> > > I want the ingredients of each product displayed in sequence with the > lowest 'mass-order' ingredient first and the highest last etc. I also > want commas between the ingredients and a full-stop after the last one > like so: > Orange juice, apple juice, banana. > > I used 'xsl:for-each' to loop through the ingredients and sorted the > ingredients by mass-order before using 'xsl:value-of' to display the > ingredient. I also added the following to the 'xsl:for-each' element: > > <xsl:choose> > <xsl:when test=". != ../ingredient[last()]"> > <span>, </span> > </xsl:when> > <xsl:otherwise> > <span>.</span> > </xsl:otherwise> > </xsl:choose> > > However I expected this to put a '.' after the last displayed > ingredient but it actually puts it after the last ingredient in the > XML mark-up so that if it is not displayed last it still has a full- > stop after it. > > Does anyone know how to achieve my goal? > > To reiterate I want the ingredients sorted and then displayed with > commas after all but the last displayed ingredient which I want to > have a '.' after it. > Use test="position() != last()" Note that sorting in xsl doesn't actually rearrange the order of the nodes in the document. It simply defines the order that the selected nodes are delivered to the next stage in the processing. -- Anthony Jones - MVP ASP/ASP.NET | ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
