Wrap an element with conditional anchor
This is an open discussion with 4 replies, filed under XSLT.
Search
<xsl:template match="slideshow/item"> <xsl:if test="not(url = '')"> <xsl:text disable-output-escaping="yes"><![CDATA[<a href="]]></xsl:text> <xsl:value-of select="url"/> <xsl:text disable-output-escaping="yes"><![CDATA[">]]></xsl:text> </xsl:if> <div id="item"> ... </div> <xsl:if test="not(url = '')"> <xsl:text disable-output-escaping="yes"><![CDATA[</a>]]></xsl:text> </xsl:if>
Right. Sorry, but I think I broke the page with my first code paste. Can't seem to edit the posts now either! I was under the false impression that you used ` to insert code.
I would't create tags in <xsl:text>
elements. It's there to output text :-)
I'd create a template to create the general html output for a slideshow item, and separate wrapper templates that apply this template. Like
<xsl:template match="slideshow/item"> <xsl:apply-templates select="." mode="output"/> </xsl:template> <xsl:template match="slideshow/item[url != '']"> <a href="{url}"> <xsl:apply-templates select="." mode="output"/> </a> </xsl:template> <xsl:template match="slideshow/item" mode="output"> <div class="slideshow-item"> ... </div> </xsl:template>
(Markdown uses ` for inline code and indentation for code blocks.)
Hi Jonas,
I would't create tags in elements. It's there to output text :-)
Yeah, I was thinking that my solution was a bit dodgy. Yours, however is exactly the sort of elegance I was looking for. Thank you!
(and also for the markdown tips)
Create an account or sign in to comment.
I'm making a slideshow and want to wrap each entire slide with a conditional anchor link (this is now cool with W3C in HTML5, by the way)
This is what I've done: ` <![CDATA[ ">
`
In these initial stage sof getting to know XSLT, I always wonder if there's a quicker/cleverer/more elegant way to do things. It does work, but can it be improved upon?