next
Subject: Problems Using XSL to OutPut Rows With Alternating Color Scheme Author: P D Date: 20 Nov 2006 09:12 AM
hi.
i am trying to output every other row a different color from my XSL
transform but i am having a tough time. the problem is that the
returned rows depends on the user's search term(s) and the path traversed within the XML tree changes from letter-to-letter within
the search (i am using onkeypress_= and ajax to return results).
i got somewhat close by creating a class "color" :
<xsl:attribute name="class">color<xsl:value-of select="position() mod 2"/></xsl:attribute>
as an attribute for my returned rows:
<xsl:value-of select="concat($path,' > ',@title)" />
the problem is that the position() function only works for the top-most parent node. as the user enters more letters to the search term (and the search decends further into the tree) the results get screwy. for example, for a search on "google", "g" gets the correct alternating scheme, but "go" , "goo", "goog", and so on up to "google" do not.
I have attached some .png files to provide a visual of this.
help much appreciated at this point.
My XSL Code Is Below (The area of concern is inside the when statement:
<xsl:when test="string($result)=''"> which is inside the following template: <xsl:template match="*[@title]">):
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"
/>
<xsl:param name="data" />
<xsl:variable name="upperCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"
/>
<xsl:variable name="lowerCase" select="'abcdefghijklmnopqrstuvwxyz'"
/>
<xsl:variable name="test"
select="translate($data,$upperCase,$lowerCase)" />
<xsl:template match="/">
<html>
<head xmlns="http://www.w3.org/1999/xhtml">
</head>
<body style="width:100%;">
<xsl:apply-templates select="*/*">
<xsl:with-param name="search" select="$test" />
<xsl:with-param name="path" select="''" />
</xsl:apply-templates>
</body>
</html>
</xsl:template>
<xsl:template match="*">
<xsl:param name="search" />
<xsl:param name="path" />
<xsl:variable name="result">
<xsl:call-template name="searching">
<xsl:with-param name="Sdeb" select="$search" />
<xsl:with-param name="Send" />
<xsl:with-param name="val" select="." />
</xsl:call-template>
</xsl:variable>
<xsl:if test="string($result)=''">
<xsl:value-of select="concat($path,' > ',.)" />
<br />
</xsl:if>
</xsl:template>
<xsl:template match="*[@title]">
<xsl:param name="search" />
<xsl:param name="path" />
<xsl:variable name="result">
<xsl:call-template name="searching">
<xsl:with-param name="Sdeb" select="$search" />
<xsl:with-param name="Send" />
<xsl:with-param name="val" select="@title" />
</xsl:call-template>
</xsl:variable>
<xsl:choose>
<xsl:when test="string($result)=''">
<tr>
<xsl:attribute name="class">color<xsl:value-of
select="position() mod 2"/>
</xsl:attribute>
<td>
<input type='checkbox' name='path' valign='bottom'>
<xsl:attribute name="value">
<xsl:value-of select="concat($path,' > ',@title)" />
</xsl:attribute>
</input>
<xsl:value-of select="concat($path,' > ',@title)" />
<br />
</td>
</tr>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="*">
<xsl:with-param name="search" select="string($result)" />
<xsl:with-param name="path" select="concat($path,' >
',@title)" />
</xsl:apply-templates>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="searching">
<xsl:param name="Sdeb" />
<xsl:param name="Send" />
<xsl:param name="val" />
<xsl:variable name="trans">
<xsl:choose>
<xsl:when test="contains($Sdeb,' ')">
<xsl:value-of select="substring-before($Sdeb,' ')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Sdeb" />
</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:variable name="word" select="string($trans)" />
<xsl:choose>
<xsl:when
test="contains(translate($val,$upperCase,$lowerCase),$word)">
<xsl:choose>
<xsl:when test="$Sdeb=$word">
<xsl:value-of select="$Send" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="searching">
<xsl:with-param name="Sdeb"
select="substring-after($Sdeb,' ')" />
<xsl:with-param name="Send" select="$Send" />
<xsl:with-param name="val" select="$val" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:choose>
<xsl:when test="$Sdeb=$word">
<xsl:value-of select="concat($Send,' ',$word)" />
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="searching">
<xsl:with-param name="Sdeb"
select="substring-after($Sdeb,' ')" />
<xsl:with-param name="Send"
select="concat($Send,'',$word)" />
<xsl:with-param name="val" select="$val" />
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
A Piece Of My XML Tree To Give You The General Idea:
<?xml version="1.0" encoding="utf-8" ?>
<categories id="00" title="main category">
- <category id="02" position="01" title="business">
<subcategory id="01" location="root" title="general">
<subsubcategory id="01" weight="0" title="CRM" />
<subsubcategory id="02" weight="0" title="Entrepreneur" />
<subsubcategory id="03" weight="0" title="European Business" />
<subsubcategory id="04" weight="0" title="Human Resources" />
<subsubcategory id="05" weight="0" title="IP and Patents" />
</subcategory>
<subcategory id="02" weight="0" title="media">
<subsubcategory id="01" weight="0" title="Advertising" />
<subsubcategory id="02" weight="0" title="Book publishing" />
<subsubcategory id="03" weight="0" title="Broadcasting" />
<subsubcategory id="04" weight="0" title="Cable" />
<subsubcategory id="05" weight="0" title="Digital television" />
<subsubcategory id="06" weight="0" title="Journalism" />
</subcategory>
<subcategory id="03" weight="0" title="companies">
<subsubcategory id="01" weight="0" title="3M" />
<subsubcategory id="01" weight="0" title="ATT" />
<subsubcategory id="01" weight="0" title="Alcoa" />
<subsubcategory id="01" weight="0" title="Altria" />
<subsubcategory id="01" weight="0" title="American Express" />
<subsubcategory id="01" weight="0" title="Boeing" />
</subcategory>
<subcategory id="04" weight="0" title="entertainment">
<subsubcategory id="01" weight="0" title="Arts and culture" />
<subsubcategory id="01" weight="0" title="Bollywood" />
<subsubcategory id="01" weight="0" title="Book reviews" />
<subsubcategory id="01" weight="0" title="TV shows" />
<subsubcategory id="01" weight="0" title="film previews" />
<subsubcategory id="01" weight="0" title="general" />
<subsubcategory id="01" weight="0" title="gossip" />
<subsubcategory id="01" weight="0" title="Movie news" />
</subcategory>
<subcategory id="14" weight="0" title="technology" />
<subcategory id="15" weight="0" title="top stories" />
</category>
<category id="03" position="01" title="news">
<subcategory id="01" weight="0" title="Iraq" />
<subcategory id="02" weight="0" title="Wall Street" />
<subcategory id="03" weight="0" title="Politics" />
</category>
[SNIP ... ]
search1.png search for "g"
search2.png search for "go"
search3.png search for "goo"
Powered by ,
the world's leading for
,
,
,
,
,
,
,
,
,
and !