Test Empty Strings

Test Empty Strings

Test Empty Strings C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string company = "<b>The Inside Corner, Inc. Benicia, CA</b>";
string address = "";

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
string displayName = Sitecore.StringUtil.GetString(item.DisplayName, item.Name);
//using &#58; for colon and &#160; for space
output.Write("{0}&#58;&#160;{1}", "Item", displayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "Company String", "<b>" + company + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Address String", "<b>" + address + "</b>");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Br);
output.RenderEndTag(); //Br
if (System.String.IsNullOrEmpty(company))
output.Write("{0}", "Company String is empty or null<br/>");
else
output.Write("{0}", "Company String is not empty or null<br/>");
if (System.String.IsNullOrEmpty(address))
output.Write("{0}", "Address String is empty or null<br/>");
else
output.Write("{0}", "Address String is not empty or null<br/>");
if (company == string.Empty)
output.Write("{0}", "Company String is empty<br/>");
else
output.Write("{0}", "Company String is not empty<br/>");
if (address == string.Empty)
output.Write("{0}", "Address String is empty<br/>");
else
output.Write("{0}", "Address String is not empty<br/>");
if (company == null)
output.Write("{0}", "Company String is null<br/>");
else
output.Write("{0}", "Company String is not null<br/>");
if (address == null)
output.Write("{0}", "Address String is null<br/>");
else
output.Write("{0}", "Address String is not null");

output.RenderEndTag(); //Div


Test Empty Strings XSLT Rendering Code

<br/>
<xsl:variable name="company" select="'The Inside Corner, Inc. Benicia, CA'" />
<xsl:variable name="address" select="''" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">
<!-- using &#58; for colon and &#160; for space -->
<p><b><xsl:value-of select="'Item'" />&#58;&#160;<sc:text field="title" select="$sc_item" /></b></p>
<xsl:value-of select="'Company String'" />&#58;&#160;<b><xsl:value-of select="$company" /></b><br/>
<xsl:value-of select="'Address String'" />&#58;&#160;<xsl:value-of select="$address" /><br/><br/>
<xsl:choose>
<xsl:when test="$company">
     <xsl:value-of select="'Company String is not empty or null'" /> <br/>
</xsl:when>
<xsl:otherwise>
     <xsl:value-of select="'Company String is empty or null'" /> <br/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$address">
     <xsl:value-of select="'Address String is not empty or null'" /> <br/>
</xsl:when>
<xsl:otherwise>
     <xsl:value-of select="'Address String is empty or null'" /> <br/>
</xsl:otherwise>
</xsl:choose>

<xsl:choose>
<xsl:when test="$company != ''">
     <xsl:value-of select="'Company String is not empty'" /> <br/>
</xsl:when>
<xsl:otherwise>
     <xsl:value-of select="'Company String is empty'" /> <br/>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="$address != ''">
     <xsl:value-of select="'Address String is not empty'" /><br/>
</xsl:when>
<xsl:otherwise>
     <xsl:value-of select="'Address String is empty'" /><br/>
</xsl:otherwise>
</xsl:choose>


Test Empty Strings C# Web Control Output

Company String: The Inside Corner, Inc. Benicia, CA
Address String: 

Company String is not empty or null
Address String is empty or null
Company String is not empty
Address String is empty
Company String is not null
Address String is not null



Test Empty Strings XSLT Rendering Output

Company String: The Inside Corner, Inc. Benicia, CA
Address String: 

Company String is not empty or null
Address String is empty or null
Company String is not empty
Address String is empty

Sources