sontek

open source hacker

Creating an XSL Extension in C#

Tags: c# xml xsl

Although XSL is very powerful, I’m sometimes more comfortable with doing certain operations in C# and luckily it is pretty simple to call C# libraries from XSL.

First, you’ll need the code that you will call from XSL, this can just be a normal POCO, doesn’t have to be anything specific to XML/XSL:

 




public class StringExtension
{
public string Proper(string data)
{
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;
return textInfo.ToTitleCase(data.ToLower());
}
}

 



Then you need to use the XsltArgumentList class to add an extension object to your Xsl Transform:



class Program
{
static void Main()
{
XPathDocument doc = new XPathDocument("data.xml");
XslCompiledTransform trans = new XslCompiledTransform();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
trans.Load("stylesheet.xslt");
XsltArgumentList args = new XsltArgumentList();
StringExtension ext = new StringExtension();
args.AddExtensionObject("ext", ext);
trans.Transform(doc, args, sw);
Console.WriteLine(sw.ToString());
Console.ReadLine();
}
}


 



Now all you have to do is register the namespace in your XSL (i.e xmlns:”ext”) and you can use the extension by calling ext:Proper():



<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
xmlns:ext="ext"
>
<xsl:output method="text" indent="yes"/>
<xsl:template match="/">
<xsl:for-each select="People/Person">
First Name:

Last Name:

</xsl:for-each>
</xsl:template>
</xsl:stylesheet>



0 Comments

Command Line Shortcuts for CMD and PowerShell

Tags: powershell windows

Here are some really useful command line shortcuts that I found on a forum awhile back but don't remember now:


F1 Repeats the letters of the last command line, one by one
F2 Displays a dialog asking user to "enter the char to copy up to" of the last command line
F3 Repeats the last command line
F4 Displays a dialog asking user to "enter the char to delete up to" of the last command line
F5 Goes back one command line
F6 Enters the traditional CTRL+Z (^z)
F7 Displays a menu with the command line history
F8 Cycles back through previous command lines (beginning with most recent)
F9 Displays a dialog asking user to enter a command number, where 0 is for first command line entered

0 Comments

ASP.NET Server tags cannot contain <% ... %> constructs

Tags: asp.net c#

We've all seen the Server Tags cannot contain <% ... %> constructs which comes from trying to use server side <% .. %> inside a server side control:


<asp:TextBox runat="server" Text="<%= ConfigurationManager.AppSettings["Title"] %>" />


You have a few ways around this, the easiest and most common way is to assign the properties server side:


this.textBox1.Text = ConfigurationManager.AppSettings["Title"];


But I like being able to do as much of my property setting in markup as possible so if I ever need to change the property it doesn't require a complete re-build/re-deploy.


The more elegant/fun solution would be to use an ExpressionBudiler, you've most likely used this class already if you've ever worked with a a multilingual website, to have controls pull globalized text from your resx resources you do the following:


<asp:Label runat="server" Text="<%$ Resources:Text, ParticipantHeader %>" />


This is using a ResourceExpressionBuilder, there is also an AppSettingsExpressionBuilder and a ConnectionStringsExpressBuilder built into the .NET framework that you can use. If these don't do what you want you can always build your own, you can see an example of a custom ExpressionBuilder at the MSDN Documentation.

1 Comments

ASP.NET 4.0 SEO Features

Tags: asp.net seo

In the upcoming release of ASP.NET 4.0 they have introduce 2 really nice features for SEO, the first one is being able to progmattically add Meta Keywords and Description to the page:


Page.MetaDescription = "Foo";
Page.MetaKeywords = "Foo, Bar, Baz";


Which will render the following in the header:
<meta name="description" content="Foo" /> <meta name="keywords" content="Foo, Bar, Baz" />


The 2nd nice feature added to ASP.NET 4.0 is 301 permanent redirects via the Response.RedirectPermanent("page.aspx") method. So now in your Global.asx.cs you can manage 302 redirects for pages who have moved like this:


protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.FilePath == "/Product/23.aspx")
{
Response.RedirectPermanent("/Products/23/", true);
}
}

This gives you the ability to easily manage files that are indexed in a search engine but have moved progrmatically rather than doing it through your webserver (which is much better for people who are using shared hosting).

3 Comments

jQuery Ribbon Control

Tags: javascript jquery

Mikael Söderström has created a ribbon control using jQuery, it is very impressive and will be greate to give your online applications a similar feel to what people are used to on the desktop. You can download it here and can see a demo here

0 Comments
Older Newer