How to test empty strings

Oxygen general issues.
sweetuv
Posts: 6
Joined: Mon Mar 24, 2008 4:05 pm

How to test empty strings

Post by sweetuv »

hi,

I am trying to print a message when a string is empty.

Code: Select all


 <xsl:variable name="nonempty_string" select="n2:Protocol/n2:FundingSource/n2:FundingSourceName"/>
<xsl:variable name="empty_string" />
<xsl:choose>
<xsl:when test="$nonempty_string != $empty_string" >
<xsl:apply-templates />
</xsl:when>
<xsl:otherwise >
<fo:inline>None</fo:inline>
</xsl:otherwise>
</xsl:choose>
This works well if the nonempty_string has a value. In other words, for an empty string I am not able to print "None".
Dan
Posts: 501
Joined: Mon Feb 03, 2003 10:56 am

Re: How to test empty strings

Post by Dan »

The variable empty_string must be initialized to an empty string. See the following example:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="nonempty_string" select="'alpha beta'"/>
<xsl:variable name="empty_string" select="''"/>
<xsl:choose>
<xsl:when test="$nonempty_string != $empty_string">
When
</xsl:when>
<xsl:otherwise>
Otherwise
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

Please consult an XSLT tutorial, like the one you may find here:
http://www.zvon.org/xxl/XSLTutorial/Boo ... index.html
Post Reply