| Subcribe via RSS

Passing Parameters into XSLT Document with ASP.NET

July 28th, 2009 | Comments Off | Posted in ASP.NET, XSL

XSL documents do a great job styling XML content and doing it quickly, but its not always easy to define parameters within. Say you have a variable within your ASP.NET page that you need to send over to your XSL stylesheet, what do we do? XslTransform and XsltArgumentList are the key. In the code below, we define a new XslTransform then go about setting our XsltArgumentList. The XsltArgumentList takes an array of parameters so feel free to load it up. Once we have all of our parameters defined, we move to loading up our XSLT document via the Load method within XslTransform. The Server.MapPath just traces our path down the directory structure to the document in question. Finally, we need to associate all our content with the asp:Xml control on the page. We load the XML up. In this case, I had it in a string object. The we transform the content with the Xsl document we defined. Next, we associate our parameters we defined.

Now let’s take a look at the XSLT document. We have to define the xsl:param node so we have a placeholder to move our value into. These parameters definitions rest in the header of the document between the xsl:stylesheet and the xsl:template tags. As you can see, we’ve named our variable ‘myVariable’ and given it a default value of ’123′ in case we don’t have a value in which to send over. This default value will be overwritten when we make our call from the ASPX page. Then all we have to do is call our variable using the xsl:value-of select. Be sure to prepend the variable name with the dollar sign so it can properly access the value.

Write to a File with CSharp (c#)

July 24th, 2009 | Comments Off | Posted in C#

This one is pretty self explanatory. You have some content you’ve gathered from a database, webservice, other means and you want to write that out to a file. We are going to call on the StreamWriter class for help. First things first, reference the System.IO class so you have all the libraries to make this work. Next, we define what the file is going to be called and where it is going to be written to. When I define the StreamWriter, I’m passing in three values 1) the file path, 2) whether I want to append the content to the existing file (true) or overwrite it (false), 3) and what form of encoding I’ll be using (UTF8 in the instance). Next, we go through and write each line out and flush it into the file. Five lines of code to take care of this indispensable function.

Opening a Popup Window from a Link via Javascript

July 20th, 2009 | Comments Off | Posted in Javascript

Fairly simple task today. I’m just looking to open a popup window using javascript. There are several different ways and complexities on how to approach this, but I found this the easiest. First, we define our window.open function in our page header section. Next, we pass in the parameters we want to set for this new window — namely the page we want to open, an identifier for that page, and a slew of properties like menubars, height, width, etc.

Now that we have all that established, its time to call the javascript function from our page. In the following we are wrapping up an image in this hyperlink for the popup. Setting the href to javascript:win1 makes our call and triggers the popup window. Simple as can be.

Capitalize First Letter of Each Word in a String via CSharp (c#)

July 10th, 2009 | Comments Off | Posted in C#

Sometimes you are parsing urls or other files and would like to be able to do something with that text. All lower case and boring isn’t going to excite many of your users. So in looking to capitalize the first letter of each word in a string we have to look beyond the bounds of the String class. The answer is in the globalization namespace. There is a method TextInfo.ToTitleCase that does just that.

That puny text becomes “Ajax The Definitive Guide.” The folks at O’Reilly would be happy that I properly emphasized their book’s title.

Delete Parent XML Node from Querying Child Attribute Using XPath & ASP.NET

July 9th, 2009 | Comments Off | Posted in ASP.NET, XML

So I’m receiving this XML string from an affiliate provider and evidently they don’t do any scrubbing on this data before kicking it my way. There are numerous instances of garbage node (i.e. test transaction – don’t put online). So I was tasked with cleaning up what they did not. I was looking to dig into the XML document and identify which child node innertext had the offensive text by querying the attribute hinged to that particular node. Take a look below at the XML structure I’m dealing with.

So in the following, say we wanted to delete all the Sleep Inn’s. We got a lumpy bed last time and the receptionist was smoking weed at the counter. Out you go Sleep Inn. The following code block accomplishes this.

So we’re using XPath to dig down to that description node. Once we are there, we create an XmlNodeList of all the nodes that fit this condition set in the XPath statement. Then we find which of these description nodes have “Sleep Inn” within their InnerText so we can define a the parentNode and remove all those child nodes. In all it scalps everything down to the <hot> tag. If we also wanted to get rid of this empty stub, we’d do a Regex statement to replace the start and end tag with blank space. Not the most elegant solution, but it works.

Working with XSLTransform and ASP:XML Controls in ASP.NET

July 7th, 2009 | Comments Off | Posted in ASP.NET, XML, XSL

Its a bit criminal how long its been since I worked with XSLT. I had this hellish project early in my programming career building a XML/XSLT program in ASP (yeah it was a while ago). I think it scarred me so bad I never had a desire to touch the stuff again. Really since you are able to read XML data straight into GridViews, there isn’t a whole lot of instances in my day to day coding life where I can’t just get by with that. Well today I had an issue where I was getting an XML datafeed from an affiliate provider and trying to find the best way to consume it. The big gotcha that kept me from going the easy GridView route was the format the data was in. It is ugly. The defining values are housed in the attributes and the node names are all the same (see below). What numbnuts designed this thing?

Anyway after trying to traverse the nodes, regenerate the XML document and other generally painful things to get the data to cooperate, I had a eureka moment to just plug it into an XSLT stylesheet. Talk about making my life incredibly easy. Here is the grand total of what it took to get the two elements plugged into my ASPX page. Notice the top 4 lines are my call to the affiliate site to get the XML dataset and line 5 is plugging the XML into the asp:XML control. Basically, one line of code. I like it.

Next, its time to dig into the XSLT. I've stripped this down to the essentials so we can focus on what is going on. Our xsl:for-each statement on line 15 drops down into the /response/result/hot node then our xsl:when tests to insure we bypass any test records the affiliate provider may have included. Next, we loop through the str node and pull out all of our relevant attributes (i.e. str[@name='description']). On line 41, we throw in our catchall for those datasets that come back without records.

All in all its pretty straight forward. I had lots of bells and whistles I had to incorporate to make this makeshift GridView work like it traditionally would under ASP.NET. Over the next few days, I'll post these extras.

Tags: , , , ,

Webhost4Life Web Hosting Sucks

July 3rd, 2009 | Comments Off | Posted in goDaddy, Web Hosting, webhost4life, WordPress

Let me tell about the week I have had, all courtesy of the incompetent techs running Webhost4life hosting service. The week starts with my Movable Type admin panel being mysteriously unavailable. The main site is still up mind you, but no way to actually edit or post anything. So the support request goes in, and they first try to change the main cgi file over to a pl file thus triggering a mountain of errors. Their response to this was, “can you figure out what all these errors are about?” You dumb idiot. You changed the file extension. This has worked as is for the past 4 years. Why do you now think changing the file extension is the answer? So another tech changes it back to cgi and gets me in. Great, but when I go to publish nothing has write access. A day goes by and now I can write to one directory, but not another. Still basically can’t post. Another day goes by, and I’m back to normal. Not so fast sparky. I get a seperate email that morning saying that they’ve switched servers on me overnight and so all my ODBC connections are now on the fritz. The site has effectively been offline for hours. It would have been nice to send me the email the day before so I could have the code gracefully fall over to the new server. Now that that is fixed, none of the image files work even though they are full qualified urls. Incompetance like this has to be rewarded. I’ve spent the last day pulling all the data down off the WebHost4Life server and loading it up to Godaddy. I’ve also put the blog on WordPress which makes my life so much easier. Moveable Type was a headache and a pain in the ass. Five years ago there wasn’t much better out there though. So that was my week.

Truthfully if this was WebHost’s first f$&k up, I could probably give them another chance, but this seems to be a perpetual issue. The site will go down for a couple to 10 minutes at a time then be right back up. No one has an explanation for what caused the hiccup. I keep running into file upload buffer limits where they have to clear something on their side. They always seem to be changing servers or screwing with something that breaks my site. Logically if you are going to have everyone point to the e drive as the web root, wouldn’t you maintain that drive letter when you relocated it to a new server? I know this is way too logical. The biggest irritant is just that they are never on top of things. They let issues sit for days at a time before responding. Customer support is not a high priority.

Please do me a favor and never use Webhost4life for your web host. You’re just asking for trouble.

Tags: ,