Andrew Howard
Andrew Howard

Reputation: 3072

XSLT if body has a class then do something

I'd like my xslt to show some html depending on whether the class of "video" displays on the body tag. Is this possible? For many reasons, I can't use Javascript.

Upvotes: 1

Views: 1201

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

In XSLT one can avoid almost completely explicit conditional instructions:

<xsl:template match=
  "/html/body[contains(concat(' ', @class, ' '),' video ')]">
 <!-- Wanted processing here -->
</xsl:template>

Of course, in order to be selected for execution, this template needs to match a node from the node-set specified in the select attribute of a corresponding <xsl:apply-templates> -- either explicitly or as part of the XSLT default processing (as part of a built-in XSLT template).

Upvotes: 5

Lukas Eder
Lukas Eder

Reputation: 220877

<xsl:if test="contains(/html/body/@class, 'video')">
</xsl:if>

Of course, this will also evaluate to true for my-video and other classes. If such collisions are possible, consider using

<xsl:if test="/html/body/@class = 'video' or
              contains(/html/body/@class, ' video ' or
              starts-with(/html/body/@class, 'video ' or
              ends-with(/html/body/@class, ' video')">
</xsl:if>

If using XSLT 2.0, you can also use the matches() function

Upvotes: 3

Related Questions