I have created a custom xslt style to display the news on my website. My issue is that some of my news have a banner and some of them don't. So how do I determine by using XSLT how to display the news with the banner or without it if it doens't have any?
I know what field contains the banner url. So I can imagine 开发者_StackOverflowI have to loop through my news and then load the display form by the return value from some sort of if-statement that checks if the news has a banner or not.
I'm new to XSLT so how do I do this? Any help is appriciated.
I think you can use something like this:
<xsl:choose>
<xsl:when test="not(empty(imageurlfield))">
<!-- Display with image here -->
</xsl:when>
<xsl:otherwise>
<!-- Display without image here -->
</xsl:otherwise>
</xsl:choose>
You can use xsl:if tag to test for some xpath existance:
<xsl:if test="./@banner">
<p class="banner"><xsl:value-of select="./@banner" /></p>
</xsl:if>
Just specify two templates, one for news items with a banner, and one without.
<xsl:template match="newsitem[banner]">
<!-- pattern for news items with a banner -->
</xsl:template>
<xsl:template match="newsitem">
<!-- pattern for news items without a banner -->
</xsl:template>
The above example assumes news items are contained in an element named newsitem
, with a child element banner
containing data about the banner, if there is one. The second template here is superceded by the first for newsitem
elements that contain a banner
child.
精彩评论