0

We have some XSLT when the value of Message is NULL the h2 transforms into a self closing tag. How can we stop this?

    <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" omit-xml-declaration="yes"/> 
    <xsl:template match="/">   <xsl:for-each select="Title"><h2><xsl:value-of select="Message"/> </h2>  </xsl:for-each>  </xsl:template> </xsl:stylesheet>

1 Answer 1

1

Wrap the message in a choose block?

<xsl:for-each select="Title">
   <xsl:choose>
    <xsl:when test="string-length(Message) &gt; 0">
        <h2><xsl:value-of select="Message"/> </h2>
    </xsl:when>
     <xsl:otherwise>
         //something to handle null or empty messages
      </xsl:otherwise> 
  </xsl:choose>  
</xsl:for-each>  
2
  • Thanks for the response, that way would be rather verbose as I need to do it in a number of places. If I was going to do it with a test I would use this code <xsl:if test="Message != ''"> <h2><xsl:value-of select="Message"/> </h2></xsl:if> Commented Jun 7, 2012 at 14:23
  • 1
    If you need to do it in a number of places, you could always make it a template and just call that template where needed. I went with choose so you could inject anything you wanted into the output like a generic message or any other html. Commented Jun 7, 2012 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.