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.

4 comments:

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

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

    ReplyDelete
  2. 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.

    ReplyDelete
  3. You're correct. Fixed the post.

    ReplyDelete