Forum Moderators: open
I have a asp application that pulls moreover news and displays it using the xsl stylesheet below. However, at the moment the way the style sheet is it displays the news like this:
<tr><td bgcolor="#FFFFFF"><A href="#">HEADLINE1</A></td></tr>
<tr><td bgcolor="#FFFFFF"><A href="#">HEADLINE2</A></td></tr>
so it basically loops the first line that has the <tr> tag. but what i wanted to do is make the second table row a different color. So for example the first headline is #ffffff the 2nd should be #cccccc and the 3rd #ffffff and the 4th #cccccc and on and on...
please can someone look at the stylesheet below and tell me how i can do this?
many thanx in advanced.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<xsl:for-each select="moreovernews/article">
<xsl:choose>
<xsl:when expr="childNumber(this) > 10"></xsl:when>
<xsl:otherwise>
<tr><td bgcolor="#FFFFFF"><A class="newsheadlines"><xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute> <b>»</b> <xsl:value-of select="headline_text"/></A></td></tr>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="moreovernews">
<table>
<xsl:for-each select="article">
<tr>
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<xsl:attribute name="class">oddrow</xsl:attribute>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
<td><a class="newsheadlines">
<xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute>
 <strong>»</strong> 
<xsl:value-of select="headline_text"/></a></td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>
tr.oddrow td {background:#e5e5e5}
it seems to work. but you've left out the following line from the orignal xsl code:
<xsl:when expr="childNumber(this) > 10"></xsl:when>
and as a result i cannot limit the numer of headlines.
can you tell me how i can add that line? i tried adding it but im getting errors.
thanx again.
thanx
The character '<' cannot be used in an attribute value.
any other suggestions?
thanx
thanx again for your help.
any ideas?
thanx
<!-- collapse whitespace in headline_text and then
count the number of characters -->
<xsl:variable name="hdlLength"
select="string-length(normalize-space(headline_text))" />
<xsl:choose>
<!-- if collapsed headline_text is 25 characters or longer,
display the first 25 characters and add an ellipsis -->
<xsl:when test="$hdlLength >= 25">
<xsl:value-of select="substring(normalize-space(headline_text),1,25)"/>
<xsl:text> . . .</xsl:text>
</xsl:when>
<!-- if collapsed headline_text is shorter than
25 characters, display it unmodified -->
<xsl:otherwise>
<xsl:value-of select="normalize-space(headline_text)" />
</xsl:otherwise>
<?xml version='1.0' encoding='utf-8'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" />
<xsl:template match="moreovernews">
<table width="274" border="0" cellspacing="0" cellpadding="0">
<xsl:for-each select="article[position() <= 10]">
<tr bgcolor="#CCCCCC">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<xsl:attribute name="bgcolor">#FFFFFF</xsl:attribute>
</xsl:when>
<xsl:otherwise />
</xsl:choose>
<td><a class="newsheadlines">
<xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute> <strong>»</strong>  <xsl:value-of select="headline_text"/></a></td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
can someone help me include the code given by choster into the code above that i currently have?
thanx in advanced.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<x****put method="html"/>
<xsl:template match="moreovernews">
<table width="274" border="0" cellspacing="0" cellpadding="0">
<xsl:for-each select="article[position() <= 10]">
<tr bgcolor="#CCCCCC">
<xsl:choose>
<xsl:when test="position() mod 2 = 1">
<xsl:attribute name="bgcolor">#FFFFFF</xsl:attribute>
</xsl:when>
<xsltherwise/>
</xsl:choose>
<td>
<a class="newsheadlines">
<xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute>
<xsl:attribute name="target">_blank</xsl:attribute> <strong>»</strong> 
<xsl:variable name="hdlLength" select="string-length(normalize-space(headline_text))"/>
<xsl:choose>
<!-- if collapsed headline_text is 25 characters or longer, display the first 25 characters and add an ellipsis -->
<xsl:when test="$hdlLength >= 25">
<xsl:value-of select="substring(normalize-space(headline_text),1,25)"/>
<xsl:text> . . .</xsl:text>
</xsl:when>
<!-- if collapsed headline_text is shorter than 25 characters, display it unmodified -->
<xsl:otherwise>
<xsl:value-of select="normalize-space(headline_text)"/>
</xsl:otherwise>
</xsl:choose>
</a>
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
thanx in advanced.
To insert characters, you need to apply the additional character in the template itself, recursively. XSLT is not designed for easy string manipulation, so if you can send the output to ASP or another tool at your disposal it would probably be more efficient. Or, if you're passing it through to another template, you could wrap it in a CDATA block.
If you're trying to escape the quotes (which it looks like you are) using XSLT, you'd have to do something like this:
[...]
<!-- replace the xsl:value-of="headline_text" bits with this block -->
<xsl:apply-templates name="escapeQuot">
<xsl:with-param name="inputString" select="substring(normalize-space(headline_text),1,25)"
</xsl:apply-templates>
[...]
</xsl:template>
<xsl:template name="escapeQuot">
<xsl:param name="inputString" />
<xsl:choose>
<xsl:when test='contains($inputString, """)'>
<xsl:value-of select='substring-before($inputString, """)'/>
<xsl:text>\</xsl:text>
<xsl:call-template name="escapeQuot">
<xsl:with-param name="inputString"
select='substring-after($inputString, """)'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$inputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:attribute name="href"><xsl:value-of select="url"/></xsl:attribute>
i tried to change it to:
<xsl:attribute name='href'><xsl:value-of select="url"/></xsl:attribute>
but made no difference.
thanx
Double or single quoting is a function of settings in your transformer engine. You cannot manipulate it directly using the current versions of XSLT or XPath, as these languages were not intended to handle such manipulation.
You can kludge a solution pretty quickly by hard-coding the quotes instead of having them generated by the processor. In my experience this often introduces problems with unescaped characters. But it is not complicated at all.
<a class='newsheadlines' href='{url}' target='blank'> <strong>»</strong> 
[...]
</a>