I am looking for a way to wrap content with xsl. This is a simplified example of what I am doing. Lot's of content ... is a significant amount of content, and the anchor tag is only used as an example. It could be a div or anything else.
XML:
<root>
<attribution>John Smith</attribution>
<attributionUrl>http://www.johnsmith.com</attributionUrl>
</root>
XSL: How I am currently doing it. This is adding a significant amount of xsl and I am sure there is a way to simplify.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:if test="attribution != ''">
<xsl:choose>
<xsl:when test="attributionUrl != ''">
<a>
<xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
<span>Thank you, <xsl:value-of select="attribution"/></span>
<div>Lots of content ...</div>
</a>
</xsl:when>
<xsl:otherwise>
<span>Thank you, <xsl:value-of select="attribution"/></span>
<div>Lots of content ...</div>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
XSL: Conceptually this is what I want to do. It doesn't work because it is invalid XML, but it does capture the idea.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:if test="attributionUrl != ''">
<a>
</xsl:if>
<xsl:attribute name="href"><xsl:value-of select="attributionUrl"/></xsl:attribute>
<span>Thank you, <xsl:value-of select="attribution"/></span>
<div>Lots of content ...</div>
<xsl:if test="attributionUrl != ''">
</a>
</xsl:if>
</xsl:template>
EDIT:
I am trying to avoid the multiple versions of <div>Lots of content ...</div>
You can have a template that does the "Thank you" stuff and use it for both cases. Using template matching instead of
<xsl:if>
or<xsl:when>
is more in the spirit of XML anyway:This should do it: