[xsl] XML to HTML: How to make decisions based on what has been display ed

Subject: [xsl] XML to HTML: How to make decisions based on what has been display ed
From: constantin.vasiliu@xxxxxxxxxxxxxx
Date: Wed, 29 Oct 2003 14:58:55 -0800
Greetings,

I am creating the stylesheet for displaying info from fairly large XML files
(1 to 2 MB). The output is HTML and I'm using the
http://www.w3.org/1999/XSL/Transform namespace. I have to alternate
background colors of every row in a table. The FAQ list has a
straightforward answer that I've used:

<xsl:choose>
	<xsl:when test="position() mod 2 = 0">
		<xsl:attribute name="BGCOLOR">white</xsl:attribute>
	</xsl:when>
	<xsl:otherwise>
		<xsl:attribute name="BGCOLOR">gainsboro</xsl:attribute>
	</xsl:otherwise>
</xsl:choose>

This should work, right? Wrong! The problem I have is that I'm not selecting
every element for the output tree. For instance, if I have five elements,
and for some reason (immaterial for the purpose of this question) I want to
select only elements 1, 3 and 5, they'll all end up being the same color. My
idea is that if I can store somehow the value of the previous background, I
can look at it and select the next one. I looked at xsl:variable, but you
can't change (or reassign) that value. I'd like to know if this can be done
at all and if so, how? If it can't be done, the source XML files will have
to be changed.

Also, I have to display in the HTML document the name - only once - of an
element who has at least one descendant satisfying certain conditions. The
way I solved that was to use xsl:for-each on the descendants, and select the
name of the element only on the first descendant satisfying the condition:

<!-- Display the test name only if the test has numeric results -->
<xsl:for-each select="preceding-sibling::ResultList/Element/Numeric">
	<!-- Display the test name only for the first encountered numeric
result (guarantees exactly one display of the test name) -->
	<xsl:if test="../preceding-sibling::Element/Numeric=false()">
		<H2><br/><br/>
			<FONT FACE="ARIAL">
				<xsl:if test="../Status = ' Passed'">
					<xsl:attribute
name="COLOR"><xsl:value-of
select="//ReportOptions/Colors/Passed"/></xsl:attribute>
					<xsl:value-of
select="../../../Sequence"/>
					<IMG
SRC="C:\TestStand\Examples\XMLReports\passed.gif"/>
				</xsl:if>
				<xsl:if test="../Status = ' Failed'">
					<xsl:attribute
name="COLOR"><xsl:value-of
select="//ReportOptions/Colors/Failed"/></xsl:attribute>
					<xsl:value-of
select="../../../Sequence"/>
					<IMG
SRC="C:\TestStand\Examples\XMLReports\failed.gif"/>
				</xsl:if>
			</FONT>
		</H2>
	</xsl:if>
</xsl:for-each>

That worked just fine until somebody threw at me an XML file that had some
additional elements defeating the purpose my checks to determine the "first"
descendant. Again, if I would have a way of knowing if the ancestor name has
already been displayed, I would be done. Wouldn't have to determine the
first descendant any more, just get a match on every descendant but display
only if the ancestor name hasn't been displayed. Can this be done?

Thanks,

CV

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread