there is a case, that appears often times. I am parsing an XML and generate my XHTML document via XSLT 1.0.
Case:
/* XML */
<Image src="path-to-img.jpg" href="link-to-page.html" />
/* XSL */
<xsl:choose>
<xsl:when test="@href">
<a href="{@href}">
<img src="{@src}"/>
</a>
</xsl:when>
<xsl:otherwise>
<img src="{@src}"/>
</xsl:otherwise>
</xsl:choose>
You see the problem: I am just fetching the case if there is a href set. I’m not satisfied with this approach, but I don’t see another option for implementing this.
Any ideas?
The way to eliminate explicit conditional instructions inside a template is to use pattern-matching within the match pattern of a template:
XSLT 2.0: There is an especially ellegant solution using
<xsl:next-match>:Both transformations, when applied on the provided XML document:
produce the wanted, correct result: