xsl-list
[Top] [All Lists]

RE: XML to HTML: How to make decisions based on what has been displayed

2003-10-30 01:36:36
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. 

This shouldn't matter. position() calculates the position of the current
node within the current node list, which is the list of nodes selected
using <xsl:apply-templates> or <xsl:for-each>. So long as you select the
nodes to be included using one of those instructions (and not, for
example, by selected a larger set of nodes and then selecting a subset
using xsl:if or xsl:choose) the position() should be set correctly.




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

You can't store anything in XSLT - it's a side-effect-free language; and
there is no concept of "previous" - it's not a sequential execution
model. 

 
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:

That shouldn't be necessary. I can't follow your code below but I think
you've made it much too difficult. From the description given above, you
should be doing something like:

<xsl:value-of select="name(element[.//*[conditions]][1])"/>

Michael Kay



<!-- 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



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



<Prev in Thread] Current Thread [Next in Thread>