Archive for September, 2006

My World of Warcraft character

Thursday, September 21st, 2006

Yeah, I’m still pretty weak, being level 24. But I am ranked now due to some fun PvP in Warsong Gulch. Here’s my profile:

http://wow.allakhazam.com/profile.html?1866549

I’m slowly going to wittle down BJ’s resolve until he starts to play. BWAHAHAHA. Then Hrubik is next.

Client-side XML transformations with Sarissa

Thursday, September 21st, 2006

(I’m not going to explain in detail what XML, XSL, or XPath are here. If you don’t know, check out w3school’s xml tutorial, xsl tutorial, and xslt tutorial)

I recently had the opportunity to work with client-side XML parsing, XSL transformations, and XPath for a project. The problem, as it usually manifests itself, is the fact that each browser tends to implement its own version of the DOM in its own way (if it implements it at all) and varying degrees of support for XPath and XSL.

I found two decent frameworks for dealing with the XML, XSL, and XPath. One is made by Google, called Google AJAXSLT. It is a pure-Javascript implementation of XSLT and XPath, so it is kind of slow, and the documentation is rather lacking. I also found a library called Sarissa which is a cross-browser wrapper for native XML handling. So it provides one interface to interact with the DOM functions but uses the underlying implementation so it is a lot faster than Google’s AJAXSLT.

It’s also easy to work with XML and you transformations. Let’s say I have a file on a webserver somewhere named test.xml, which looks like this:

<books>
  <book>
    <author>J.K. Rowling</author>
    <title>Harry Potter and the Sorcerer's Stone</title>
    <isbn>0439554934</isbn>
  </book>
  <book>
    <author>J.K. Rowling</author>
    <title>Harry Potter and the Chamber of Secrets</title>
    <isbn>0439554896</isbn>
  </book>
  <book>
    <author>J.K. Rowling</author>
    <title>Harry Potter and the Prizoner of Azkaban</title>
    <isbn>043965548X</isbn>
  </book>
</books>

And say you have an XSL stylesheet named test.xsl that looks like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h1>My Favorite Books</h1>
    <table border="1">
      <tr bgcolor="#CCCCCC">
        <th>Author</th>
        <th>Title</th>
        <th>ISBN</th>
      </tr>
      <xsl:for-each select="books/book">
      <tr>
        <td><xsl:value-of select="author" /></td>
        <td><xsl:value-of select="title" /></td>
        <td><xsl:value-of select="isbn" /></td>
      </tr>
      </xsl:for-each>
    </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

You can transform the XML by loading the Sarissa library and the Sarissa DHTML functions library in your HTML head:

<script type="text/javascript" src="sarissa.js"></script>
<script type="text/javascript" src="sarissa_dhtml.js"></script>

sarissa.js is required for the base Sarissa methods, and the sarissa_dhtml.js contains some AJAX tricks to update the content based on your XML document and your stylesheet.

I start by creating a simple Javascript function to get an XML document. It is sort of like an AJAX call, but it is synchronous because I want to block further action until I get the document. So its more like SJAX or just JAX. Yeah, that’s it…

function getXmlDocument(fileUrl)
{
    var xmlDocument = Sarissa.getDomDocument();
    xmlDocument.async = false;
    xmlDocument.load(fileUrl);
    if (Sarissa.getParseErrorText(xmlDocument) != Sarissa.PARSED_OK)
    {
	    alert(Sarissa.getParseErrorText(xmlDocument));
    }
	return xmlDocument;
}

All that does is creates a new DOM Document via Sarissa, and loads an XML file as defined in the parameter fileUrl. Next I create an init() function that runs when the HTML document loads. It is responsible for loading the XML file and the XSL stylesheet, and doing the actual transformation. Here it is:

function init()
{
	var xml       = getXmlDocument("test.xml");
	var xsl       = getXmlDocument("test.xsl");
	var processor = new XSLTProcessor();
	var results   = document.getElementById('results');

	processor.importStylesheet(xsl);
	Sarissa.updateContentFromNode(xml, results, processor);
}

My HTML page looks like this:

<body onload="init()">
  <div id="results"> </div>
</body>

Now that all the pieces are in place, when you load the HTML page, you end up with something like this:

My Favorite Books

Now, granted, you could always embed a link to an XSL stylesheet in the XML file and call it directly from an XSL compatible browser (i.e. most modern major browsers) but this is neat for applications where you want to have some web 2.0 goodness and allow some manipulation of the data, searching via xpath, or whatever. You could use this to create HTML from RSS feeds, format data retrieved from a web service, or whatever you can think of. Keep in mind that doing XML transformations can require a lot of resources depending on the size of your XML document, so don’t push a 500 MB XML document to the client in order to let it do the transformations. Keep it lightweight, user friendly, and provide accessibility alternatives for people with older browsers.

A Slow Weekend

Monday, September 18th, 2006

This weekend wasn’t terribly interesting, but it was still a good one. My dad and I spent eight hours on Saturday installing a garage door opener for my sister Kristin, and after that I was dead tired and sore from crawling around her attic, lifting stuff, and the general agony of work. I had enough time to clean up and crash for a bit and then Lori and I went over to Hope’s apartment, who was having a party for our friend Carol who was offered a new job. We ate pizza, drank sodas, and generally had a good time.

It’s amazing how much a little caffeine can take you from being totally exhausted to being as giddy as a school girl.

And by a little caffeine I mean a freaking ton of caffeine.

Sunday, in contrast, was much more relaxed. It rained, so a family reunion on Lori’s mom’s side was cancelled, so we slept in and played video games. We finally decided to get out and went to lunch and then to Sam’s Club to get stocked up on some groceries. There we ran into some friends from high school we hadn’t seen in five years (Joe and Allyssa Zeder) and talked to them a bit before returning home with our groceries. After that my sister Lisa and her family stopped by to say hello. Then we watched some episodes of Naruto and went out to eat dinner with Heathe and Laura Yeakley, ending the evening with a trip to Borders.

Sure, it seems like a lot of mundane details, but really it is important to me because before we moved back to Oklahoma, a weekend like this wouldn’t have been possible. We would have been too stressed out because of travelling to really enjoy the time with family and friends, and we couldn’t just randomly go see people or help out with projects around the house all at the same time. So what I’m saying is that its nice being back.

Under Pressure

Tuesday, September 12th, 2006

I’m writing this under the pressure of my peers. I guess I need to update ye olde blarg a bit more often so people don’t think I’ve succumbed to WoW-caholism.

So what have I been up to lately? Work mainly, but my free time has been spent helping my sister Lisa and her family move back to Oklahoma, helping my sister Kristin fix up her house a bit, and playing games. Lots and lots of games.

I’ve spent a lot of time playing Battlefield 2, Half-Life 2, Prey, and World of Warcraft. The latter has been dominating here lately. I’m now a level 23 gnome warlock on a PvP server, and I’m starting to get a hang of the PvP scene. I’ve also joined a guild (Optimus Luminarium), and even managed to get Lori hooked on playing. If anyone is interested, I’m playing on the Dark Iron server (as is Lori and my brother Andrew). It’s also the server that the Penny Arcade alliance guilds are on, as well as the people from Ctrl-Alt-Del. The game is an incredible amount of fun, so if you have the opportunity, you should pick it up. Seriously. You can get a 10-day free trial if you want. The first one’s always free. :)