Forum Moderators: open

Message Too Old, No Replies

XSL stylesheet

how to make a table row loop

         

laura2k

12:50 pm on Jan 12, 2004 (gmt 0)


Hi,

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>&#160;<b>&#187;</b>&#160;<xsl:value-of select="headline_text"/></A></td></tr>

</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

choster

2:37 pm on Jan 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is how you would do it in a "regular" transformer, there may be a shortcut in Microsoft's engine.
<?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>
&#160;<strong>&#187;</strong>&#160;
<xsl:value-of select="headline_text"/></a></td>
</xsl:for-each>
</tr>
</table>
</xsl:template>
</xsl:stylesheet>

Then in the output document you would just define a CSS style for the cells in the highlighted row:

tr.oddrow td {background:#e5e5e5}

laura2k

2:59 pm on Jan 12, 2004 (gmt 0)



thanx choster,

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.

macrost

3:10 pm on Jan 12, 2004 (gmt 0)

10+ Year Member



laura2k,
In your original xsl, you are using the old and defunct namespace. While that one had some good things about it, like a counter (kinda), it wasn't really supported. Let me write something up that will limit the number of rows that are being written in the output.

Mac

laura2k

3:26 pm on Jan 12, 2004 (gmt 0)



Thanx mac, i will really appreciate it if you can tell me how i can limit the number of rows that are being written in the output

thanx

choster

4:02 pm on Jan 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I missed that. I'm not familiar with Microsoft's XSL engine, so I don't know what "expr" is. If you want to just display whatever are the first ten nodes, try changing <xsl:for-each select="article"> to <xsl:for-each select="article[position()<11]">.

laura2k

4:05 pm on Jan 12, 2004 (gmt 0)



i tried that and i am getting the following error:

The character '<' cannot be used in an attribute value.

any other suggestions?

thanx

choster

4:08 pm on Jan 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Arrrgh my bad. One more time: <xsl:for-each select="article[position() &lt;= 10]">

laura2k

4:11 pm on Jan 12, 2004 (gmt 0)



it works now.

thanx again for your help.

laura2k

6:27 pm on Jan 12, 2004 (gmt 0)



is there anyway to limit the width aswell? i want to limit the headline lenght and maybe add a ... to it.

any ideas?

thanx

choster

8:06 pm on Jan 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you wanted to restrict the display to, say, the first 25 characters, you can use XPath to select a substring of headline_text. I think it would look something like this (replaces the <xsl:value-of select="headline_text"/>):

<!-- 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 &gt;= 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>

The normalize-space is optional, but I think it's good practice for newsfeeds-- those I've worked with are not too careful about formatting. You wouldn't want "Sky Is Blue" rendered with an ellipsis just because the Reuters operator keyed it in with twenty spaces between "Sky" and "Is."

laura2k

5:10 pm on Jan 13, 2004 (gmt 0)



im having trouble including the above code you gave me into my current code. my current code is as follows:

<?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() &lt;= 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>&#160;<strong>&#187;</strong>&#160; <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.

macrost

6:00 pm on Jan 13, 2004 (gmt 0)

10+ Year Member




<?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() &lt;= 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>&#160;<strong>&#187;</strong>&#160;
<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 &gt;= 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>

laura2k

6:18 pm on Jan 13, 2004 (gmt 0)



thanx, it worked

laura2k

9:55 pm on Jan 14, 2004 (gmt 0)



the above code restricts the number of characters allowed. Can anyone tell me how i can replace certain characters. for example if the headline contains the " symbol it should replace it to \"

thanx in advanced.

choster

10:05 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Laura, read up on XPath, for instance the translate() function. It will answer most or all of your questions.

choster

3:25 pm on Jan 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got your Sticky, larua2k, I misunderstood your question. You're trying to replace a quotation mark with a backslash and quotation mark, which cannot be done with XPath alone; translate() simply replaces one character for another.

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, "&quot;")'>
<xsl:value-of select='substring-before($inputString, "&quot;")'/>
<xsl:text>\</xsl:text>
<xsl:call-template name="escapeQuot">
<xsl:with-param name="inputString"
select='substring-after($inputString, "&quot;")'/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$inputString"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

laura2k

11:17 pm on Jan 16, 2004 (gmt 0)



is there anyway to change the character that is used in the href tag? for example at the moment the line below seems to return <a href="link"> is there anyway to change the " character to '

<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

choster

3:27 pm on Jan 20, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey laura, I was out of town for the holiday weekend and didn't get your sticky until this morning.

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'>&#160;<strong>&#187;</strong>&#160;
[...]
</a>

laura2k

10:41 pm on Jan 20, 2004 (gmt 0)



i tried that, but i am still get the " characters instead of the ' characters