Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Easy way to alternate output with XSLT when not using templates or loops?

Reply
Thread Tools

Easy way to alternate output with XSLT when not using templates or loops?

 
 
Michael Ahlers
Guest
Posts: n/a
 
      07-12-2004
Obviously if you're looping or using a template, choosing output based
on the current iteration is easy. For example, if you're walking a
set of elements and you want index % 2 == 0 produce one thing and
another when that is not true. My question is, what if you are not
looping through tags but want to achieve a similar effect without
having to be excessively verbose? Is there any way that I can
simplify the following block?

<!-- ... -->
<dt>
<xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
<xsl:text>Sex</xsl:text>
</dt>
<dd>
<xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
<xsl:value-of select="Sex"/>
</dd>
<dt>
<xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
<xsl:text>Height</xsl:text>
</dt>
<dd>
<xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
<xsl:value-of select="Height"/>
</dd>
<!-- ... -->

That's particularly annoying if you want to insert an element anywhere
other than the end of the list. You cannot modify variables, so
incrementing as you walk along is not possible. I can't exactly loop
because the name of the element is not necessarily human-friendly.
Any ideas?
 
Reply With Quote
 
 
 
 
Robin Johnson
Guest
Posts: n/a
 
      07-15-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Michael Ahlers) wrote in message news:<(E-Mail Removed). com>...
> Is there any way that I can
> simplify the following block?
>
> <!-- ... -->
> <dt>
> <xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
> <xsl:text>Sex</xsl:text>
> </dt>
> <dd>
> <xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
> <xsl:value-of select="Sex"/>
> </dd>
> <dt>
> <xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
> <xsl:text>Height</xsl:text>
> </dt>
> <dd>
> <xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
> <xsl:value-of select="Height"/>
> </dd>


If Sex and Height (or whatever data) are the only child elements of the context:

<xsl:for-each select="*"><!-- or maybe select="Sex|Height|whatever..." -->
<xsl:variable name="even-or-odd">
<xsl:choose>
<xsl:when test="position() mod 2 = 0">
<xsl:text>Even</xsl:text>
</xsl:when>
<xsltherwise>
<xsl:text>Odd</xsl:text>
</xsltherwise>
</xsl:choose>
</xsl:variable>
<dt class="{$even-or-odd}">
<xsl:value-of select="name()"/>
</dt>
<dd class="{$even-or-odd}">
<xsl:value-of select="."/>
</dd>
</xsl:for-each>

--
Robin Johnson
http://www.robinjohnson.f9.co.uk - rj at robinjohnson dot f9 dot co dot uk
"The labours of men of genius, however erroneously directed, scarcely ever fail
in contributing ultimately to the solid advantage of mankind." - Mary Shelley
 
Reply With Quote
 
 
 
 
Michael Ahlers
Guest
Posts: n/a
 
      07-15-2004
> If Sex and Height (or whatever data) are the only child
> elements of the context:


It would be extremely convenient I only had those two tags since this
whole thing would be effortless. The problem is, I only took a snippet
of my code to demonstrate what I am shooting for. Also, I have no
guarantee that the element names are suitable for user output, so I have
to supply them myself in the XSLT. Thanks for your response though.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Reply With Quote
 
David Carlisle
Guest
Posts: n/a
 
      07-15-2004
(E-Mail Removed) (Michael Ahlers) writes:

>
> <!-- ... -->
> <dt>
> <xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
> <xsl:text>Sex</xsl:text>
> </dt>
> <dd>
> <xsl:attribute name="class"><xsl:text>Even</xsl:text></xsl:attribute>
> <xsl:value-of select="Sex"/>
> </dd>
> <dt>
> <xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
> <xsl:text>Height</xsl:text>
> </dt>
> <dd>
> <xsl:attribute name="class"><xsl:text>Odd</xsl:text></xsl:attribute>
> <xsl:value-of select="Height"/>
> </dd>
> <!-- ... -->
>


The above is written in a very verbose style, you could write it
equivalently as



<!-- ... -->
<dt class="even">Sex</dt>
<dd class="even">
<xsl:value-of select="Sex"/>
</dd>
<dt class="odd">Height</dt>
<dd class="odd">
<xsl:value-of select="Height"/>
</dd>
<!-- ... -->

or if you want to loop over arbitrary child elements

<xsl:for-each select="*">
<dt class="even">
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">odd</xsl:attribute>
</xsl:if>
<xsl:apply-templates mode="name" select="."/>
</dt>
<dd class="even">
<xsl:if test="position() mod 2 = 1">
<xsl:attribute name="class">odd</xsl:attribute>
</xsl:if>
<xsl:apply-templates select="."/>
</dd>
</xsl:for-each>


for names you can have a default of

<xsl:template mode="name" match="*">
<xsl:value-of select="name()"/>
</xsl:template>

together with overrides where needed:

<xsl:template mode="name" match="Sex">
<xsl:text>seX</xsl:text>
</xsl:template>


David
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what's 'alternate' in <link rel=alternate>? Chris ASP .Net 2 03-04-2007 04:45 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Alternate initializers or alternate class? transfire@gmail.com Ruby 10 07-17-2006 03:20 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM
Direct output to alternate location bear Javascript 0 10-21-2003 01:02 PM



Advertisments