473,603 Members | 2,635 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:text>Sex </xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:value-of select="Sex"/>
</dd>
<dt>
<xsl:attribut e name="class"><x sl:text>Odd</xsl:text></xsl:attribute>
<xsl:text>Heigh t</xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl: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?
Jul 20 '05 #1
3 2342
mi******@elwing .org (Michael Ahlers) wrote in message news:<9d******* *************** ****@posting.go ogle.com>...
Is there any way that I can
simplify the following block?

<!-- ... -->
<dt>
<xsl:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:text>Sex </xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:value-of select="Sex"/>
</dd>
<dt>
<xsl:attribut e name="class"><x sl:text>Odd</xsl:text></xsl:attribute>
<xsl:text>Heigh t</xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl: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|Hei ght|whatever... " -->
<xsl:variable name="even-or-odd">
<xsl:choose>
<xsl:when test="position( ) mod 2 = 0">
<xsl:text>Eve n</xsl:text>
</xsl:when>
<xsl:otherwis e>
<xsl:text>Odd </xsl:text>
</xsl:otherwise>
</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
Jul 20 '05 #2
> 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!
Jul 20 '05 #3
mi******@elwing .org (Michael Ahlers) writes:

<!-- ... -->
<dt>
<xsl:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:text>Sex </xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl:text>Even</xsl:text></xsl:attribute>
<xsl:value-of select="Sex"/>
</dd>
<dt>
<xsl:attribut e name="class"><x sl:text>Odd</xsl:text></xsl:attribute>
<xsl:text>Heigh t</xsl:text>
</dt>
<dd>
<xsl:attribut e name="class"><x sl: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">Se x</dt>
<dd class="even">
<xsl:value-of select="Sex"/>
</dd>
<dt class="odd">Hei ght</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:attribut e name="class">od d</xsl:attribute>
</xsl:if>
<xsl:apply-templates mode="name" select="."/>
</dt>
<dd class="even">
<xsl:if test="position( ) mod 2 = 1">
<xsl:attribut e name="class">od d</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
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2080
by: Dimitre Novatchev | last post by:
You seem to be unaware of the xslt processing which uses the built-in rules in the absence of templates that match some selected node. http://www.w3.org/TR/xslt#built-in-rule According to the XSLT processing model: http://www.w3.org/TR/xslt#section-Processing-Model the root node will be processed by a built-in rule, because you do not
1
5574
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML tags and French accented characters in the records. I have specified <xsl:output method="html"/> and encoding="iso-8859-1". When I apply the xsl:value-of and set the disable-output-escaping to "yes", the HTML formatting tags are displayed correctly, but the French accented characters are...
1
1671
by: Harry Zoroc | last post by:
I would like to treat an xsd Schema file as XML file and to display the targetNamespace and all the imports. That's it. But the following does not work. Why? I did not enter the stylesheet in the xsd file directly but tried to compute the output on the command line e.g. with xalan like: java net.sf.saxon.Transform -o myout.html myschema.xsd myxsltfile.xslt Using Saxon yields no better result. The produced myout.html contains all HTML...
2
2328
by: intrepidca | last post by:
When I try to translate an XML file (using org.apache.xalan.xslt.Process) that has a DOCTYPE declaration, I only get the <?xml ...?> processing instruction in the output file. I get no error messages. If I remove the DOCTYPE declaration it works fine. I have checked that the XML file is valid according to the DTD (using xmllint) and that checks out. Here are snippets from the XML and the XSL files and debugging output from xalan: the...
4
1491
by: Lord0 | last post by:
Hi there, Is the following possible with XSLT? Given the following example XML docs: <!-- doc 1--> <user> <username>myUsername</username> <password></password> <phone>12345</phone>
4
1908
by: Stan | last post by:
Forgive a newbie, please: I've got XML like this: <body> <block> <p>content of p1</p> <p>content of p2</p> <p>content of p3</p> ... <p>content of pN</p>
3
9575
by: super.raddish | last post by:
Greetings, I am relatively new to, what I would call, advanced XSLT/XPath and I am after some advice from those in the know. I am attempting to figure out a mechanism within XSLT to compare the difference between two source documents and output node-sets which are "different" (changed or new) to new XML files using xsl:result-document To describe the problem I have provided some example data below along with my a portion of my current...
1
3450
by: CAM123 | last post by:
I have added: <br><xsl:value-of select="Line" /></br> to my XSLT stylesheet to get a line per repeating block. When I view the output as XML it looks perfect - one line per block. However when I output the file to a text file, all the data is wrapping and at the end of each block I am getting the text part of the header included but not all of it. The text that appears is: <br xmlns:msxsl="urn:schemas-microsoft-com:xslt"...
6
3722
by: John Larson | last post by:
Hi All, I am some information from INSPEC database records in XML to build a relational database of my own. I am currently trying to extract information by doing an XSLT transform of the XML files into a tab-separated text file that I want to import into the database. I have run into the following problem: in some documents there are missing elements, for instance the volume and issue number of an article is not there (i.e. it is defined...
0
7996
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8405
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8060
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8273
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
5878
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3951
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2430
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1259
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.