Dressing with xsl
I am starting to think that the range code is not the problem. For some reason, the counting is not correct. Can it be either in xml or in ok. 150 from case studies? Or could it be in the order in which I do the for-each and if?
<!-- XML -->
<case-studies>
<case-study>
<name>Company A</name>
<solutionType>Mobility</solutionType>
<solutionType>Convergence</solutionType>
<solution category="Business services">Product</solution>
<solution category="Business services">Industry</solution>
<solution category="#">A-Z</solution>
<product>Product 1</product>
</case-study>
</case-studies>
I am trying to create a table that alternates between gray rows within each one. The code below returns a stylish effect.
thanks
<xsl:sort select="../name" />
<xsl:if test="@category[. = $solName]">
<tr>
<xsl:if test="(position() mod 2 = 1)">
<xsl:attribute name="bgcolor">#e7e7e7</xsl:attribute>
</xsl:if>
<td class="cell1">
<img src="/images/icons/infoWhite.gif" style="margin:3px 3px 0 0px;" id="{../name}" onmouseover="xstooltip_show('{../url}', '{../name}', 0, 10);" onmouseout="xstooltip_hide('{../url}');" />
<div id="{../url}" class="xstooltip" style="margin:10px 0 0 10px;">
<div class="floatLeft"><strong>Product(s):</strong></div>
<div class="margLeft10 floatLeft">
<xsl:for-each select="../product/prodName">
<div class="clearRight"><xsl:value-of select="."/></div>
</xsl:for-each>
</div>
</div>
</td>
<td class="cell2" style="padding-top:2px;">Β» <a href="{../url}"><xsl:value-of select="../name"/></a></td>
<td class="cell3">
<xsl:for-each select="../solutionType">
<div class="clearRight"><xsl:value-of select="."/></div>
</xsl:for-each>
</td>
</tr>
</xsl:if>
</xsl:for-each>
a source to share
<xsl:for-each select="NewDataSet/authors">
<xsl:sort select="au_lname"/>
<xsl:if test="position() mod 2 = 1">
<tr bgcolor="#aaccff">
<td><xsl:value-of select="au_lname"/></td>
<td><xsl:value-of select="au_fname"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:if>
<xsl:if test="position() mod 2 = 0">
<tr bgcolor="#ffccaa">
<td><xsl:value-of select="au_lname"/></td>
<td><xsl:value-of select="au_fname"/></td>
<td><xsl:value-of select="phone"/></td>
</tr>
</xsl:if>
</xsl:for-each>
a source to share
Looking at your XSLT, it prompts you to iterate over "solution" elements in. Is it correct?
I think the problem is that when you check position (), it will take to all previous decision elements, regardless of whether the previous ones were ignored by the condition.
I can offer two possibilities for solving this problem.
First, try changing the test for position () to count () for the number of matching previous elements. For instance:
<xsl:if test="(count(preceding-sibling::solution[@category=$solName]) mod 2 = 0)">
Alternatively, you can try adding a test in @category to the state and removing the condition. For instance:
<xsl:for-each select="solution[@category = $solName]">
<xsl:sort select="../name" />
<tr>
<xsl:if test="(position() mod 2 = 1)">
Hope this makes sense!
a source to share
The problem is what you need to use xsl:element
to work properly xsl:attribute
. You cannot set the attribute of a static element. You should get xslt to create the item for you.
<xsl:sort select="../name" />
<xsl:if test="@category[. = $solName]">
<!-- use xsl:element to create an element -->
<xsl:element name="tr">
<xsl:if test="position() mod 2 = 1">
<!-- then xsl:attribute will function as expected -->
<xsl:attribute name="bgcolor">#e7e7e7</xsl:attribute>
</xsl:if>
<!-- snip -->
</xsl:element>
</xsl:if>
</xsl:for-each>
I would suggest that instead of inline style names, you use class names and css (background-color):
<xsl:sort select="../name" />
<xsl:if test="@category[. = $solName]">
<!-- use xsl:element to create an element -->
<xsl:element name="tr">
<xsl:if test="position() mod 2 = 1">
<!-- then xsl:attribute will function as expected -->
<xsl:attribute name="class">alternateRow</xsl:attribute>
</xsl:if>
<!-- snip -->
</xsl:element>
</xsl:if>
CSS example (for my alternative solution):
.alternateRow { background-color: #e7e7e7; }
Plus, you don't need to put parentheses around your tests. If I have multiple tests, I wrap them in parentheses for readability, but with only one condition, parent entries are not added deliberately.
a source to share