Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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>
share|improve this question
add comment

1 Answer

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>  
share|improve this answer
    
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> –  Colin Murray Jun 7 '12 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. –  PirateEric Jun 7 '12 at 14:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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