Subject: Re: [xsl] XML to HTML: How to make decisions based on what has been display ed From: Wendell Piez <wapiez@xxxxxxxxxxxxxxxx> Date: Wed, 29 Oct 2003 18:49:08 -0500 |
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.
<set> <element status="okay">Whee</element> <element status="okay">Hoo</element> <element status="not okay">Boo!</element> <element status="okay">Yip</element> <element status="okay">Zing</element> </set>
<xsl:template match="set"> <xsl:apply-templates/> </xsl:template>
<xsl:template match="element"> <xsl:if test="@status = 'okay'"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="position()"/> </xsl:if> </xsl:template>
<xsl:template match="set"> <xsl:apply-templates select="element[@status='okay']"/> <xsl:text>
</xsl:text> </xsl:template>
<xsl:template match="element"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="position()"/> <xsl:text>
</xsl:text> </xsl:template>
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?
Cheers, Wendell
====================================================================== Wendell Piez mailto:wapiez@xxxxxxxxxxxxxxxx Mulberry Technologies, Inc. http://www.mulberrytech.com 17 West Jefferson Street Direct Phone: 301/315-9635 Suite 207 Phone: 301/315-9631 Rockville, MD 20850 Fax: 301/315-8285 ---------------------------------------------------------------------- Mulberry Technologies: A Consultancy Specializing in SGML and XML ======================================================================
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] XML to HTML: How to make deci, constantin . vasiliu | Thread | RE: [xsl] XML to HTML: How to make , Michael Kay |
[xsl] Converting specific child ele, Sindigi, Ganesh K | Date | Re: [xsl] retaining "xml:" prefix o, Saverio Perugini |
Month |