Using scripts in XSLT

by Svelmoe 2. June 2009 18:36

I mentioned in my last blog post that you can use scripts in XSLT, so I just wanted to show shortly how you can use scripts to show the power it can yield.
Firstly – add a namespace to your XSLT file:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                xmlns:scripts="urn:scripts.this">

Then you can use the scripts in various situations.
For example, I had to parse some "Yes" and "No" into Boolean strings "true" and "false" (for business logic reasons) so I made a script for that in the bottom of my XSLT file (I made the scripts in C# syntax):

<msxsl:script language="C#" implements-prefix="scripts">
    <![CDATA[
        //return true or false as string instead of "yes" and "no"
    public string getBooleanString(string s0)
    {
      if ((String.Equals(s0, "yes", StringComparison.OrdinalIgnoreCase))
      { return "true"; }
      if (String.Equals(s0, "no", StringComparison.OrdinalIgnoreCase))
      { return "false"; }
      return s0;
    }
    ]]>
</msxsl:script>

Now, I could use this script in my XSLT matching like this:

<xsl:value-of select ="scripts:getBooleanString(./VALUE)"/>

This makes a call to the C# script “getBooleanString” with the value, the script will test it for “Yes” and “No” and return “true” or “false” respectively, or the string itself it it wasn’t one of the two.
Some of the other things I used this for was to compare strings case with case insensitive, and to hold temporary values to avoid having to run through my XSLT file multiple time in the transformation process.  I also had to convert some number formats to use period as decimal point instead (due to language specific settings), so I made a script which changes between period and comma.

It is a powerful tool to have in the XSLT toolbox.

XSLT in .NET

by Svelmoe 1. June 2009 13:39

I have been working quite a bit with doing XSLT on some large XML files lately.
During this I worked with the XslCompiledTransform class in .NET, which make transformations really easy and it works very fast.

An advantage of this class is also that it enables you to utilize scripts within your XSLT file, which enables you to make for example C# functions, for various tasks. I’ll write more about that in a later blog entry to keep this one simple and to the point.

The syntax (in vb.net) for doing a transformation using XslCompiledTransform is as simple as the following:

    'Create settings which eanble scripting in the XSLT file
    
Dim xmlSettings As New 
XsltSettings
    xmlSettings.EnableScript 
= True
    
'create a XML reader for the XLST file
    
Dim xsltFile As XmlReader XmlReader.Create("XSLT FILE"
)
    
'create a Transform object
    
Dim xslTransform As New 
XslCompiledTransform
    
'Load XSLT file with Settings into transform object
    
xslTransform.Load(xsltFilexmlSettingsNothing
)
    
'Do the transformation.
    
xslTransform.Transform("INPUT XML FILE""OUTPUT XML FILE")
  

The main disadvantage I’ve found for using this transformation is the huge memory usage it uses.
In my own situation, the process used about 3.5 times as much memory as my source XML file, meaning that a source file about 450MB my .NET process was killed on a 32 bit computer with an out of memory exception.

The actual memory usage will of course vary depending on how complex your transformation is, so the 3.5 number is not meant to be nothing more than an indicator that it is memory intensive.
It is however very fast, and I managed to transform very large files in a manner of seconds, thus if you can live with the memory consumption it is quite effective.

String comparison performance, and regular expression, in vb.net

by Svelmoe 26. May 2009 16:50

Having to do some optimizing of processes over the last period of time, I decided to do some quick benchmark of the various string comparisons, to see how they performened against each other.
The ones I was interested in was the String.Equal and String.Compare, so I decided to make some quick testing.

I made a WinForm and used the following code to test with:

      For i As Integer = 0 To 100000
            If String.Equals("item", "ITEM", StringComparison.InvariantCultureIgnoreCase) Then
                Debug.WriteLine("yes")
            End If
        Next
        For i As Integer = 0 To 100000
            If String.Equals("item", "ITEM", StringComparison.CurrentCultureIgnoreCase) Then
                Debug.WriteLine("yes")
            End If
        Next
        For i As Integer = 0 To 100000
            If String.Equals("item", "ITEM", StringComparison.OrdinalIgnoreCase) Then
                Debug.WriteLine("yes")
            End If
        Next
        For i As Integer = 0 To 100000
            If String.Compare("item", "ITEM", True) = 0 Then
                Debug.WriteLine("yes")
            End If
        Next
        For i As Integer = 0 To 100000
            If String.CompareOrdinal("item", "item") = 0 Then
                Debug.WriteLine("yes")
            End If
        Next
        Dim regex As New System.Text.RegularExpressions.Regex("item", System.Text.RegularExpressions.RegexOptions.Compiled Or System.Text.RegularExpressions.RegexOptions.IgnoreCase)
        For i As Integer = 0 To 100000
            If regex.IsMatch("item") Then
                Debug.WriteLine("yes")
            End If
        Next


The specifics of the code aren’t that interesting, it was mostly to create some comparable numbers, so I use just one string “item” to compare to “ITEM” (except for CompareOrdinal, which is mostly there as a benchmark).

Anyways I was quite surprised at the numbers when I ran the code through RedGate’s ANTS performance profiler.



For the String.Equals with the various options, the following numbers were meassured:
StringComparison.InvariantCultureIgnoreCase gave 51.490ms (the actual number is irrelevant, it is the relative difference I’m interested in)

StringComparison.CurrentCultureIgnoreCase gave 65.747ms which is close enough to the above that I’ll say those two are quite similar.

StringComparison.OrdinalIgnoreCase however only took 12.378 for the same amount of iterations and true checks. This surprised me a bit.

String.Compare took 66.004ms, which is similar to the CurrentCultureIgnoreCase.

CompareOrdinal only took 8.056ms, but of course is case sensitive, so it doesn’t quite match up to this situation (unless you spend less time running a ToUpper on your compare clauses).

RegEx.IsMatch took a staggering 375.127ms with compiled and ignore clause, but then again – you shouldn’t use regular expression for something as trivial as this anyway. Regular expression is a very powerful tool in its own right, this was just to compare.

The big surprise is that there is such a big difference between using StringComparison.OrdinalIgnoreCase in your String.Equals compared to the other options. I’ll refer to the msdn documentation for the specifics of the different types (http://msdn.microsoft.com/en-us/library/system.stringcomparison.aspx) , but shallowly said - the Ordinal as the name says ignores culture specific matching.

So if that is acceptable in a given situation, using String.Equals with OrdinalIgnoreCase is by far the best option when comparing strings.

Again note the numbers aren’t numerically important, the relative difference existed as I ran the code several times, with OrdinalIgnoreCase winning out significantly each time.
In a production function I had made, I was able to shave several percent off my execution time, simply by switching from String.Compare to String.Equals with OrdinalIgnoreCase option used.
That was an easy way of gaining performance in my book.

VB.NET and the WITH statement

by Svelmoe 20. April 2009 20:23

I love using the WITH statement in VB.NET/ASP.NET because it eases up on the typing and helps “group together” functionality.
However, I found a fun little something when using the WITH statement, which I wanted to share.

It seems that you can assign a new object reference within a WITH statement to the specific object, however you can’t assign values to said new reference. Strange indeed, but I’m sure there’s some underlying reason.
Here some code to illustrate.

Suppose I have a class:

Private Class Test
        Private m_Text As String

        Public Property TextValue() As String
            Get
                Return m_Text
            End Get
            Set(ByVal value As String)
                m_Text = value
            End Set
        End Property
End Class

Then I have some code which uses this class in a WITH statement:

Dim obj As New Test()
obj.TextValue = "Test"
  For i As Integer = 0 To 10
   With obj
     If Not String.IsNullOrEmpty(.TextValue) Then
          System.Diagnostics.Debug.WriteLine("VALUE: " & .TextValue)
    Else
         System.Diagnostics.Debug.WriteLine("VALUE NOTHING")
    End If
    obj = New Test()
    .TextValue = "test" & i
   End With
Next

When run this provides the output:
VALUE: Test
VALUE NOTHING

And "nothing" for every subsequent run.
So it is possible to assign a new reference to the “obj” object reference, but the assignment of the new TextValue is ignored.

Not that usefull, but fun enough in my book :) (yes, I’m a geek

Celebrating stupidity – Game shows and Reality TV

by Svelmoe 27. March 2009 19:31

Recently I’ve grown increasingly tired of television and the fact that stupidity and mediocrity is celebrated to the degree it is. The most annoying thing is that we pay good money for the privilege to have half-assed game shows or reality programs with people whose only prerequisite is – well, what’s the opposite of talent and smarts.

Not a few days ago I was channel surfing and stumbled upon a game show where some random person had to tell answer truthfully to some (very) private questions, and they’d win money if the lie detector matched their answers. 

Of course these people are casted so the ones with most dirty laundry are accepted – nobody would reward me for sitting there revealing mundane secrets: “Are you afraid of spiders?”, “yes”, “congratulations you’ve won XXX money”.
Anyways – the questions I’ve been able to bring myself to watch before moving on in despair for the human race, was about how the person had been cheating on a significant other, or taken drugs, or stolen from friends, or been in jail for violence or.....
Okay, one thing is bringing yourself to tell these things on public television – but what struck me as most annoying, most insane, most absurd – was the audience reaction.
They applauded and cheered the person on when answering truthfully. “Have you stolen money from somebody who’ve helped you get out of drug problems?”, “Yes”, queue audience cheering and applauding.

One thing is these sad people get on TV, another is celebrating them. Who’s the culprit – the ones visiting the show, getting paid for being messed up;  Or the ones cheering them on?
As much as I dislike the stupidity of the people going on the shows – I dislike the audiences even more. The people watching and the people reading the tabloids for new exciting information about the breakfast of contestants, and what not.

Another show I saw recently was somebody who where in economical problems. Now granted that is serious problems – but the real kicker here is that they earned so much money to begin with. They were rather wealthy and still used a load more than they could afford.
What about instead of using resources on wealthy people, the networks take those money and spend on the real unfortunate ones. The ones without job, without income, actually living on the street. But I digress – that isn’t TV worthy and doesn’t pull in ratings – but somebody spending 10 grand more than the 50 they already earn each month is?
Seriously....what is up with this?

Well sure, some credit is due, some TV shows do celebrate talent and brains and what not, and reward those – however it does not seem to be what is focused on even in those instances most of the time anyway.

Take something like the “singing contests” shows which we see en mass now in all countries and in various forms. Who do people turn in to see? The people who’re awful at it, getting chewed out by the judges – how we celebrate their stupidity. Quiz shows, then we still root for them to fail, to make a fool of themselves.  Because then we’ll all feel much better about ourselves. Beauty contestants answering poorly regarding atlas and education is celebrated online.
Survival shows or Big Brother or lock somebody away in a luxury hotel where they’ll have to vote each other off – oh the human drama aspect, the conflicts which pop up – that draw people in.

Are our own lives getting to be so damn boring that we have to celebrate the stupidity for it to somehow make our own lives more bearable or is it some sort of vindictiveness. “They’re not better than me; see how awful/stupid/evil they act/are”.

And strangely enough – I just saw the movie "Live!" ..... What a view into the future of television, and when thinking about it, the movie suddenly doesn’t seem that farfetched. Is it just a picture of things to come? Most likely.

Project: Gaming computer anno 98-2000 part 2

by Svelmoe 5. March 2009 07:37

So, finally got my VGA to DVI adapter only to find out that my monitor already supported VGA connection (d’oh), and then I got my old computer booted up.
Seems it contained a 700 MHz CPU and 256 RAM with a couple of gigs of hard drive. Plenty of power for my needs – perhaps a little too much for some of my old games, but we’ll see.

However it was extremely unstable, chrasing and locking up left and right and booting up very slowly – time to dig out a Win98 disc and reformat I think. Phew – can’t remember last time I reformatted a computer instead of just buying a new harddrive :D

Some other problems I noticed right off the bat was that even though there is a soundcard installed, it didn’t seem to produce any sound. So I will have to check whether it is installed properly, and if it even works.
The CPU fan made quite a lot of noise (dust problem I hope), so will have to look at that as well.
I’ll need to identify the graphics card, so I can install a proper version of direct X (5 or 6 most likely) which I’ll have to pull from an old disc, as I do not intend the computer to connect to the internet.
The running of a CD yielded a BSOD, which I hope will be fixed with a reformat – otherwise I might have to look into a new CD drive (properly around 4-16x speed) or burn my original CDs to new discs so I can test if it is scratches which cause the problem. Some of these CDs are old – back to mid 90s.
I’ll need to find drivers online for various things and get them burned down and installed. For example – drivers for my old Joystick (which is a Wingman Interceptor - best joystick ever), the soundcard (if it works), the graphic card and what else I find.

So there are still plenty of things to do to get it working properly – but with a bit of luck – I have the entire machine already build and only software problems remain and I don’t have to worry about hardware.

A problem with the web page

by Svelmoe 3. March 2009 07:14

Here in Denmark we received a new tax structure just recently, and one of the items was that people could cash in a sort of “forced tax savings”, which had been 1% over a number of years when the economy was moving very fast. The economical aspect of this isn’t terrible interesting (for this discussion).

But apparently the interest for doing this have been so large that it has crashed the website of the organization which controls this forced saving, because people were interested in seeing how much money they had in their forced savings.
That is the true problem of the internet in its given form. Anything extraordinary happens – and websites can’t handle the traffic.  This has been seen multiple times throughout the years, and it is basically a distributed denial of service attack. Sites can’t handle traffic and shuts down. I tried this morning and could still not access the web site, and I tried yesterday around noon. It is a good thing it isn’t a critical service.

I remember back in 2001 when the twin towers were hit. Every news site I knew was down and where impossible to reach. Suddenly the television news channels received a significant boost, because people couldn’t get their news online and we were forced back to early 90s instead.

Imagine something important happens and you have to get access to your home bank, but the bank is down because everybody else is trying to access it as well. Not a comforting thought.

As more and more of our services and daily life starts to be accessible mainly (only) via the internet – this is a problem which will escalate.
Question is if the companies who aren’t ready will want to pay for being proactive with such things, or they’ll just ignore the problem because it is “extraordinary” events which cause it ….. well, extraordinary or just people wanting to get some money.

 

New project: Gaming computer anno 98-2000

by Svelmoe 2. March 2009 12:36

In a nostalgic spur of the moment – I’ve decided to try and build up a gaming computer to run games from the late 90s.

I’m especially interested in the Wing Commander/privateer series and other of these "interactive movie" games, Masters of Magic and a number of adventure games, I have stored.

Because I like many of the old games, I follow initiatives such as Good Old Games and support it by buying some of the games. But because I have a number of the games still laying around on CD’s – unfortunately I seem to have lost/misplaced my floppy – I thought I might as well try and boot them up properly.

So my first order of business will be to identify what kind of hardware I actually have laying around the place. I basically have one full computer, but I have no idea what specs it contains, so I need to get it booted up somehow – meaning I have to invest in a VGA to DVI adapter so I (hopefully) can run it on my LCD monitor at home. Have to get a hold of a PS/2 mouse and keyboard, well the keyboard I have, unsure about the mouse. But to judge the hardware I don’t really need the mouse.

I also have an old Wingman Extreme joystick lying around – really curious if it still works as it was a wonderful joystick.

But time will tell – but I do hope to manage to boot up some of my old games again without having to rely on DOSBox or similar.

Sex in advertisement. Why does it still work?

by Svelmoe 24. February 2009 21:49

I drove past a(nother) billboard the other day of a naked woman advertising for some random drink.
It got me to thinking – why the heck does sex sell in advertisement.
Of course it must work – somehow – because otherwise, why would the advertisement agencies continue to do it.

It can’t be because of the “shock value”. I mean nudity is so common place – at least in this country – that seeing it on a billboard is strangely familiar.
Television, Internet, Tabloid newspapers, Magazines and so on almost all feature nakedness and sexual suggestive content.
Even a gadget magazine features sexual inspired content with scantily clad women posing suggestively with a few of the gadgets placed in the picture.
Sometime I wonder what the magazine is actually about when viewing the cover.

Actually though – I do understand the psychology behind it, or at least some of it.
It is because “we” (supposedly) identify with the situation and wants to experience it, and think that by buying said item – we will.
So when Barcadi features beautiful people dancing on a nightclub set and wriggling their bodies in a somewhat sexual manner – it is because the advertisement wants us to believe that by drinking Barcardi we’ll experience the same.

But come on – wouldn’t most everybody know this by now. That it is such mechanics the adverts play on?
I’ve been on many a party in my days and nothing like that ever happened .... Just because somebody started drinking a specific brand of alcohol.

It is the same for these billboards adverts – it is the same for the gadget magazines – and it is the same for the tabloid newspapers.
If they feature sexual inspired content and suggestive images, somehow people must connect the - otherwise unconnected and irrelevant – dots and fall for this. I just can’t understand it. I specifically avoid brands when their advertisements annoys me, and I know I am most likely not special in that way – so does it still work?

And what ever happened to the informative aspect of advertisements, magazines and newspapers .... well okay – the newspapers are tabloid, so they’ve never been informative to begin with.
But when I crack open a gadget magazine I want to know about gadgets.
If I view an advert about a product I want to know why on earth I should spend my money on that advert over perhaps a competitor.
And seeing a naked woman in an advertisement somehow isn’t a convincing argument (anymore).

Now I’m by no means a prude or anything, frankly I don’t care as I am not affected – I just can’t understand why sexual (more or less suggestive) content still sells in this day and age.
Or perhaps it is simply because we’ve seen it all and those slick advertisement agencies have no clue on how to move forward.

Saving that newspaper

by Svelmoe 11. February 2009 08:04

I saw the editorial in Time Magazine by Walter Isaacson about how to save newspapers.
Basically the premise is that newspapers are going under, or cancelling out “good journalism” to save money because fewer and fewer are buying the physical papers but reading the content online for free.
His solution is to get people to pay “micropayments” for reading news-material on the web, a small cost per article or something like that.
Now granted, I agree with him on a number of situations, but I question the methods.
Yes, journalists must be paid to do journalistic work. Naturally.
Yes, newspapers are currently too depending on advertisement companies then their reader because their revenue comes from advertisement. This can create a conflict of interests.
But that’s about as much in agreement as I can be.

I see many problems with starting to charge for online reading of news.
1) It only takes one of two companies to shoot down everybody else. If a few companies doesn’t charge for their content, then people will just go to the free alternatives and leave behind the ones you have to pay for. This is how most online services have worked.

2) Legality. If everybody starts charging, then I’m all but sure that several “gray” providers will pop up. It happens with everything online and it is a problem, but it is also a reality. Gaming, Music and Movie industry suffers from this. Some people spend their free time ripping off content providers and posting it up for free. That would open up for a whole can of “allofmp3” or “piratebay” problems. Charging for content opens up for those doors and the cost of keeping your content yours might prove way to high to be cost-effective.

3) By charging (more) for content, people will likely stop reading a multitude of different providers.
Personally- when I bought newspapers I read perhaps two at the most. Now when I read news online, I read about 8 different news sites. This provides me with a better chance of getting an unbiased picture of events and forming my own opinion. If I were to be charged for the content, I doubt I’d read more than a couple again.
And anybody who’ve seen Fox News for example, knows how unbiased “news” providers can be. I think it’ll hurt.
Now granted, this is also a risk under the current model cause if many providers go bankrupt, you are faced with fewer avenues through the physical medias, but that still mean there are free alternatives online.

4) By charging, you start implying that only those who can afford it, are allowed to read the news coverage. It can quickly create a divided segment where those who can’t afford to go through multiple sites either as in item 3) sticks with 1 or 2 or none at all.

5) Journalism will be much more entertainment then news. One of the arguments was to make journalists dependant on the readers and not the advertiser. Well nice and idealistic goal, but what do we usually see when a content provider becomes depending on its subscribers. Much more populism and sensationalism. It becomes content for entertainment and not enlightenment. These companies will want to attract most people, and well – unfortunately that usually means catering to the lowest common denominator. That will counter the “good journalistic principles” it was meant to promote.

6) Country barriers. If providers start charging online, then they must remember that it is global. I read occasionally US and UK news sites. If I were to pay, would I be allowed to? Or will it be like iTunes where I can only buy from my local store and not from the US or UK store?
The web is global, and the world is as well. But payments aren’t always.

7) Nationally subsidized alternatives. In this country – and many others – we have national supported, public service, channels we already pay for – either via taxes or license fee. Payment we can’t avoid. If we also were to start paying, or pay more, for other content providers, we’re back in item 4. People will leave them behind because they can get their news coverage from the public service channels. It will be a problem for competition.

Now, I do not have the answers – but I would think the way forward would be to offer a split plan. Subscription and free. Subscribe if you want to know more, more in depth articles, forums to talk to the editors and journalists and so on – but keep some content free, if nothing else to lure people into it.
Otherwise it’ll start to spell doom for the majority of content providers in a state we haven’t seen yet. Look at the music industry and how well it fares because they have problems adjusting to a global and intangible reality of the internet – and that is a more physical product which you can keep and take with you. News is much more intangible and fleeting.
Micro transactions on its own? I think it’ll be a big step in bring the industry all the way down, or weed out so we have one or two mega-corporations.

About Svelmoe

My real name is Allan Svelmøe Hansen.

I live in Denmark, where I work as a developer for hedal:kruse:brohus using SQL Server and the .NET framework since 2004. Svelmoe.dk is a place for my every day thoughts and reactions and the occasional technical blog entry.

I also blog about SQL and MS SQL Server at www.execsql.com so in case you are looking for more about that, please visit that website.



View Allan Svelmøe Hansen's profile on LinkedIn     

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010 Svelmoe.dk