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...
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
:If there is no attribute
something
present, then the if-clause already evaluates totrue
and the expression@something!='hidden'
does not have to be evaluated.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 asomething
attribute at all. Not having this attribute is entirely different from@something!='hidden'
. So, the string "something" is not output if thesomething
attribute is not present.Because of this you need to test whether there is a
something
attribute before yourxsl:if
condition is evaluated.Input
Stylesheet
EDIT @Tim C has suggested an even shorter version:
Output