lajarre
lajarre

Reputation: 5162

XSLT if - attribute equals string

I'm using a xsl:if to perform a small condition in a larger xsl:template block, and I'd like to test equality of an attribute of the current xsl:template matched node.

The following is not working:

<xsl:template match="sometag[@type='sometype']">
    ==Sometag==
    <xsl:if test="@something!='hidden'">something</xsl:if>
    <!--a lot of other stuff that I don't want to duplicate by multiplying the xsl:templates-->
<xsl:template>

This test seems to be always evaluating to false, maybe I don't have the good syntax?

This XML:

<sometag type="sometype" something="visible"/>
<sometag type="sometype" something="hidden"/>
<sometag type="sometype"/>

Should give

==Sometag==
something...
==Sometag==
==Sometag==
something...

Upvotes: 9

Views: 111562

Answers (2)

ikellenberger
ikellenberger

Reputation: 86

Based on the solution above (of @Mathias Müller and as suggested by @Tim C) you can even speed up things a little bit using Short-Circuit Evaluation for the or:

<xsl:template match="sometag[@type='sometype']">
    <xsl:text>==Sometag==</xsl:text>
    <xsl:if test="not(@something) or @something!='hidden'">
        <xsl:text>something</xsl:text>
    </xsl:if>
</xsl:template>

If there is no attribute something present, then the if-clause already evaluates to true and the expression @something!='hidden' does not have to be evaluated.

Upvotes: 1

Mathias M&#252;ller
Mathias M&#252;ller

Reputation: 22617

the 2nd tag should not get the "something" part printed.

I am not entirely sure what you are trying to achieve, but I'll give it a try.

One of your sometag elements does not have a something attribute at all. Not having this attribute is entirely different from @something!='hidden'. So, the string "something" is not output if the something attribute is not present.

Because of this you need to test whether there is a something attribute before your xsl:if condition is evaluated.

Input

<?xml version="1.0" encoding="utf-8"?>
<root>
<sometag type="sometype" something="visible"/>
<sometag type="sometype" something="hidden"/>
<sometag type="sometype"/>
</root>

Stylesheet

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

   <xsl:output method="text"/>
   <xsl:strip-space elements="*"/>

   <xsl:template match="/root">
      <xsl:apply-templates/>
   </xsl:template>

   <xsl:template match="sometag[@type='sometype']">
      <xsl:text>==Sometag==</xsl:text>
      <xsl:choose>
         <xsl:when test="@something">
            <xsl:if test="@something!='hidden'">
               <xsl:text>something</xsl:text>
            </xsl:if>
         </xsl:when>
         <xsl:otherwise>
            <xsl:text>something</xsl:text>
         </xsl:otherwise>
      </xsl:choose>
   </xsl:template>

</xsl:stylesheet>

EDIT @Tim C has suggested an even shorter version:

 <xsl:template match="sometag[@type='sometype']">
    <xsl:text>==Sometag==</xsl:text>
    <xsl:if test="@something!='hidden' or not(@something)">
       <xsl:text>something</xsl:text>
    </xsl:if>
 </xsl:template>

Output

==Sometag==something==Sometag====Sometag==something

Upvotes: 14

Related Questions