Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Subject: How can I use XSLT / Xpath to sort elements for display and treat the last one displayed differently?

From: "Anthony Jones" <Ant@------------.--->
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




transparent
Print
Mail
Like It
Disclaimer
.

These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on www.altova.com/list/index.html. Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.

.
.

transparent

transparent