Test for empty attributes in XSLT

Testing for missing attributes in XML elements is different from testing for attributes with empty values. You can test for missing attributes with <xsl:if test="not(@attribute)">, but this test will never succeed if the attribute is present but empty. In that case, you have to use the <xsl:if test="@attribute = ''"> condition.

This post is part of You've asked for it series of articles.


Anonymous said...

then how to you check if the attribute value is not emtpy
like this maybe

xsl:if test="@attribute != ''"

Ivan Pepelnjak said...

Absolutely right.

Jason N said...

Forgive me if I'm wrong, I'm new to xslt but LiquidXml editor does not work with your example above - testing for a missing attribute. This is what I got to work:
<xsl:if test="not(@attribute)">
Apparently "not" is a function and needs the parenthesis to work right.

Ivan Pepelnjak said...

You're correct. Fixed the post.

Post a Comment