Home. 
.

transparent

transparent

transparent

Altova Mailing List Archives


Re: Rendering problem

From: "N. Demos" <stormbringer_seven@-------.--->
To: 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.


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