VextoR
VextoR

Reputation: 5165

Can I check condition in template match in XSLT?

I want to check variable in template match, is it possible?

like:

 <xsl:template match="*:Item and $MODE='PURCHASE'">

So template should check variable $MODE='PURCHASE' as well

Upvotes: 1

Views: 144

Answers (1)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243509

Not in XSLT 1.0.

In XSLT 2.0 one can have variable references -- in the predicates of the template match pattern.

For example:

<xsl:stylesheet version="2.0"   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>

    <xsl:param name="MODE" select="'PURCHASE'"/>

 <xsl:template match="*:Item[$MODE='PURCHASE']">
     <xsl:value-of select="."/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on this XML document:

<t xmlns:x="some:x">
  <x:Item>someText</x:Item>
</t>

the wanted, correct result is produced:

  someText

Upvotes: 1

Related Questions