Code


Regular Expressions are a always tricky and sometimes they stir the mind. I was constructing a regular expression for following conditions:

- String should min of 3 characters and max of 6 characters

- First three characters can be [a-zA-Z]

- Either of 4,5 or 6th character should be [0-9]

- Remaining characters can be [a-zA-Z] but not [0-9]

This query troubled me like anything. I tried so many combinations but could not make it. The main problem was making sure that there is one number in the 4,5 or 6th position and limiting it to a max of 6 and min of 4 characters in length.

I came across a guy who has dveloped an online RegEx Tester and he solved the problem in no time. You can use the online RegEx Tester thru this link.

So finally the RegEx is as follows:

^(?=.*[0-9])(?!.*[0-9].*[0-9])(?!.*[0-9].*[0-9].*[0-9])[a-zA-Z]{3}[a-zA-Z0-9]{1,3}$

The above regex works for the specified condions and mes sure that there is only one digit in the string.

Following is an analysis of the same:

The RegEx two blocks: first block checks whether a certain pattern exists in the whole string and second block checks for [a-zA-Z] three times and [a-zA-Z0-9] one to three times.

^ and $ make sure that the pattern is verified from beginning to the end of the given string.

The first block:

(?=.*[0-9])(?!.*[0-9].*[0-9])(?!.*[0-9].*[0-9].*[0-9])

The ?= looks ahead in the whole string whether there exists any character of [0-9]. The ?! looks ahead in the whole string whether there is not two or three [0-9]. That means this block makes sure that the whole string contains only one digit [0-9] and not more than that. But it will not verify where exactly the [0-9] should come. It will be taken care by the second block.

The second block:

[a-zA-Z]{3}[a-zA-Z0-9]{1,3}

In the second block, first part verifies whether the first three characters are [a-zA-Z] once it is satisfied, second part checks the next 1-3 characters contain [a-zA-Z0-9].

Isn’t that a wonderful RegEx.. :)

You can visit this link for a quick reference on Regular Expressions.

If you have any regular expression that is eating your head then probably this forum can solve it.. give it a try.

I found a better a way to modify the HTML in ASP.Net just before it is sent to the client. Here is the code:

public partial class CustomRender : System.Web.UI.Page
{
 HtmlTextWriter textWriter = null;
 StringWriter stringWriter = new StringWriter();

 protected override HtmlTextWriter CreateHtmlTextWriter(TextWriter tw)
 {
 textWriter = base.CreateHtmlTextWriter(tw);
 return base.CreateHtmlTextWriter(stringWriter);
 }
protected override void Render(HtmlTextWriter writer)
 {
base.Render(writer);
 string htmlCode = stringWriter.ToString().Replace("[CurDateTime]", DateTime.Now.ToString());;

 textWriter.Write(textWriter);

 }
}

I used the same .aspx file we used in the last example.

There can be scenarios in which you will have to make some modifications to the HTML code generated by ASP.Net engine just before it is sent to the browser.

Here is how you can do it.

Create a new ASP.Net WebForm. Add a label control and set the Text as [CurDateTime]. Following is the code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CustomRender.aspx.cs" Inherits="CustomRender" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Untitled Page</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <asp:Label ID="lblTest" runat="server" Text="[CurDateTime]"></asp:Label>
 </div>
 </form>
</body>
</html>

In the Code-behind file, override the Render() event and make the changes to the Html as required. Following is the code for the same.

protected override void Render(HtmlTextWriter writer)
 {
MemoryStream mStream = new MemoryStream();
 HtmlTextWriter htmlWriter = new HtmlTextWriter(new StreamWriter(mStream));

 base.Render(htmlWriter);
 htmlWriter.Flush();
 mStream.Position = 0;

 StreamReader reader = new StreamReader(mStream);
 string htmlDump = reader.ReadToEnd();
 reader.Close();
 htmlDump = htmlDump.Replace("[CurDateTime]", DateTime.Now.ToString());
 Response.Write(htmlDump);

 }

The code replaces the Text [CurDateTime] with the current date and time in the HTML and renders it in the browser.

This is just an example you can go ahead and do as many tweaks as you want..!

In continuation to my XSLT work, I had to ratin all the HTML tags which were part of the XML elements. For example consider following XML document

<root>

<element>

Text goes here with bullets <ul><li>Item 1<li><li>Item 2</li></ul>

</element>

</root>

Here, if we capture the element with the following line in XSLT, all the HTML tags will be stripped off.

<xsl:value-of select=”element”/>

That is result will not shown as a bullet list, rather it will be shown as plain text.

Instead if you use following code in XSLT, all your HTML tags will be retained.

<xsl:copy-of select=”element”/>

Reason being: “copy-of” Creates a copy of the current node (with child nodes and attributes) where as “value-of” Extracts the value of a selected node.

Here is the bottom line: If you want to extract the value use “value-of”, if you want the value present in the xml element as it is, use copy-of.

Here is a list of XSLT Elements for your quick reference.

I was working on a piece of code in C# and wnated to apply XSLT on an object of XmlDocument. Following is the code to achieve the same 

Language: C#

.Net Framework Used: 2.0

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
namespace XmlUtils {
public class XsltTransformer {
public string TransformXmlWithXslt(string fXmlPath, string fXsltFilePath){
XmlDocument objXmlDocument = new XmlDocument();
objXmlDocument.LoadXml(fXmlPath);
XslCompiledTransform objXslTransform = new XslCompiledTransform();
objXslTransform.Load(fXsltFilePath);
StringBuilder sb = new StringBuilder();
StringWriter swriter = new StringWriter(sb);
XmlTextWriter writer = new XmlTextWriter(swriter);
objXslTransform.Transform(objXmlDocument, null, writer);
return sb.ToString();
}
}
}

 

using System;

using System.IO;

using System.Text;

using System.Xml;

using System.Xml.Xsl;

 

namespace XmlUtils {

public class XsltTransformer {

 

public string TransformXmlWithXslt(string fXmlPath, string fXsltFilePath){

XmlDocument objXmlDocument = new XmlDocument();

 

objXmlDocument.LoadXml(fXmlPath);

XslCompiledTransform objXslTransform = new XslCompiledTransform();

objXslTransform.Load(fXsltFilePath);

 

 

 

StringBuilder sb = new StringBuilder();

StringWriter swriter = new StringWriter(sb);

XmlTextWriter writer = new XmlTextWriter(swriter);

objXslTransform.Transform(objXmlDocument, null, writer);

 

return sb.ToString();

}

}

}