Forum Moderators: open

Message Too Old, No Replies

<xsl:value-of. within a tag?

         

ahmedtheking

9:04 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what I'm attempting:

<xsl:for-each select="rss/channel/item">
<li><span class="inner">[b]<a title="<xsl:value-of select="title"/> - <xsl:value-of select="description"/>" href="<xsl:value-of select="link"/>">[/b]<span class="title"><span class="date"><xsl:value-of select="pubdate"/></span> <xsl:value-of select="title"/></span> <span class="des"><xsl:value-of select="description"/> ... Read More</span></a></span></li>
</xsl:for-each>

Now you can see that the <a> tag isn't formed well. What can I do so that I can use the XSL (XML) values within the <a> tag?

ahmedtheking

9:06 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did a bit of googling:

You use XSL attr tags:

<A>

<xsl:attribute name="href">
<xsl:value-of select="link"/>
</xsl:attribute>
<xsl:value-of select="section"/>

</A>

Another example of setting XML data equal to attributes within a tag:

<img>
<xsl:attribute name="src">
<xsl:value-of select="image" />
</xsl:attribute>
<xsl:attribute name="border">
0
</xsl:attribute>

<xsl:attribute name="width">
80
</xsl:attribute>

<xsl:attribute name="height">
100
</xsl:attribute>

</img>

john_k

9:18 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have found that always using xsl:element tags is the best way to avoid issues like this.


<xsl:for-each select="rss/channel/item">
<xsl:element name="li">
<xsl:element name="span">
<xsl:attribute name="class">inner</xsl:attribute>
<xsl:element name="a">
<xsl:attribute name="title"><xsl:value-of select="title"/></xsl:attribute>
-
<xsl:attribute name="title"><xsl:value-of select="description"/></xsl:attribute>
<xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
<xsl:element name="span">
<xsl:attribute name="class">title</xsl:attribute>
<xsl:element name="span">
<xsl:attribute name="class">date</xsl:attribute>
<xsl:value-of select="pubdate"/>
</xsl:element>
<xsl:value-of select="title"/>
</xsl:element>
<xsl:element name="span">
<xsl:attribute name="class">des</xsl:attribute>
<xsl:value-of select="description"/> ... Read More
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:element>
</xsl:for-each>

ahmedtheking

9:24 pm on Apr 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is a good idea, but it can get a bit confusing!

choster

4:45 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I find there's almost never a need to use <xsl:element> unless you're going to use a variable or param to define the name of the element. When hand-coding, it's much easier to keep track of nesting with distinctive element names as opposed to counting </xsl:element>s.

As far as inserting values into attributes, why not just use bracket notation?

<xsl:for-each select="rss/channel/item">
<li><span class="inner"><a title="{title} - {description}" href="{link}"><span class="title"><span class="date"> <xsl:value-of select="pubdate"/></span> <xsl:value-of select="title"/></span> <span class="des"><xsl:value-of select="description"/> ... Read More</span></a></span></li>
</xsl:for-each>

ahmedtheking

9:48 pm on Apr 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't know you could use those brackets! Any other tips?