Archive for category Web development
Transforming XML/XSLT using .NET 3.5
Posted by Martin in ASP.NET/C#, Web development, XML/XSLT on December 14th, 2009
Here is a simple example of how you can apply a XSLT stylesheet to a XML file and deliver the result to the browser using ASP.NET 3.5.
default.aspx.cs
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class _default : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) {
// Source paths
string xmlFilePath = Server.MapPath("hello-world.xml");
string xsltFilePath = Server.MapPath("hello-world.xslt");
// Load XML
XPathNavigator nav = new XPathDocument(new XmlTextReader(xmlFilePath)).CreateNavigator();
// Stream for the transformed XML
MemoryStream stream = new MemoryStream();
// Load XSLT & transform
XslCompiledTransform transformer = new XslCompiledTransform();
transformer.Load(xsltFilePath);
transformer.Transform(nav, null, stream);
// Write output
using (StreamReader reader = new StreamReader(stream)) {
stream.Position = 0;
Response.Write(reader.ReadToEnd());
reader.Close();
}
}
}
Yes, that’s it! Here below are the rest of the code used to complete a fully working demo.
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="_default" %>
hello-world.xml
<?xml version="1.0" encoding="iso-8859-1"?> <?xml-stylesheet type="text/xsl" href="hello-world.xslt"?> <text>Hello World!</text>
hello-world.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" />
<xsl:output method="html"/>
<xsl:template match="/">
<xsl:text disable-output-escaping="yes">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title><xsl:value-of select="text"/></title>
</head>
<body>
<h1><xsl:value-of select="text"/></h1>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Tip! To have the HTML doctype declared in your XSLT document you need to wrap it in a <xsl:text> tag with the disable-output-escaping attribute set to yes. And don’t forget to swap the leading and tailing < and > for < and >.
Output
<!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>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
iPhone Scroll To bookmarklet
Posted by Martin in Browsers, JavaScript, Web development on October 12th, 2009
Do you also get tiered of flickering your finger to scroll up and down loooong web pages? I was, so I wrote this JavaScript bookmarklet to do it for me. It enables you to enter a %-value where to (vertically) scroll on the page.
How to use it:
Just add and save a bookmark on any page. Open it for edit. Name it “Scroll to …” (or what ever) and paiste the JavaScript code below into the URL field.
javascript:window.scroll(window.pageXOffset,(prompt(%22Scroll%20to%20...%20(0-100)%22)/100)*document.body.clientHeight);
To use it, just select the bookmark from the bookmarks menu, enter a value between 0-100 (where 0 is the page top and 100 is the page bottom) into the prompt and hit OK.
Happy scrolling!
Output the source XML with XSLT
Posted by Martin in Tip, Web development on October 9th, 2009
Today I just learned something I’ve been looking for for a while. It’s super simple, but it can be very helpful. For those of us that needs to write and edit XSLT for XML you don’t own and can’t view – in my case from SharePoint. Here is a single line of XSLT that will output the source XML to your page.
<xsl:copy-of select="."/>
That’s it! I told you, super simple.
Remember that you will probably not see the code on the page since the nodes probably won’t be recognized by the browser.
iPhone view source
Posted by Martin in Browsers, JavaScript, Tip, Web development on September 28th, 2009
For you who are a web developer and would like to be able to “view source” on a web page with your iPhone Safari.
Create a bookmark. Edit it and paise the code below in the location field. Then when you want to use it, just click the bookmark, and you’ll get the source. Super easy!!
javascript:var%20sourceWindow%20%3D%20window.open%28%27about%3Ablank%27%29%3B%20%0Avar%20newDoc%20%3D%20sourceWindow.document%3B%20%0AnewDoc.open%28%29%3B%20%0AnewDoc.write%28%27%3Chtml%3E%3Chead%3E%3Ctitle%3ESource%20of%20%27%20%2B%20document.location.href%20%2B%20%27%3C/title%3E%3C/head%3E%3Cbody%3E%3C/body%3E%3C/html%3E%27%29%3B%20%0AnewDoc.close%28%29%3B%20%0Avar%20pre%20%3D%20newDoc.body.appendChild%28newDoc.createElement%28%22pre%22%29%29%3B%20%0Apre.appendChild%28newDoc.createTextNode%28document.documentElement.innerHTML%29%29%3B
JavaScript for…in
Posted by Martin in ASP.NET/C#, JavaScript, Mootools, Web development on September 13th, 2009
A couple of weeks ago a college of mine got a JavaScript error at one of our SharePoint sites. It turn out, someone at Microsoft has used a for…in loop to iterate over an Array. This works fine as long as you don’t modify the Array object. So when used with Mootools, that adds properties and methods to the Array object, the SharePoint script gets the hick-ups and dies.
The JavaScript documentation states:
Iterates a specified variable over all the properties of an object, in arbitrary order. For each distinct property, the specified statement is executed.
… and
Although it may be tempting to use this as a way to iterate over Array elements, because the for…in statement iterates over user-defined properties in addition to the array elements, if you modify the Array object, such as adding custom properties or methods, the for…in statement will return the name of your user-defined properties in addition to the numeric indexes. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.
I made a simple example to show the difference between iterating over Arrays and Objects. This is with Mootols included on the page.
JavaScript
console.log('===== Array Example ===== ');
// Array
var a = ['alpha', 'bravo', 'charlie'];
console.log('Creating a new Array:', a);
console.log('The array length is:', a.length);
// For
console.log('--- Start For --- ');
for (var i = 0, j = a.length; i < j; i++) {
console.log(i, ':', a[i]);
}
console.log('--- End For --- ');
// For-in
console.log('--- Start For-in --- ');
for (var ii in a) {
console.log(ii, ':', a[ii]);
}
console.log('--- End For-in --- ');
Output
===== Array Example ===== Creating a new Array: ["alpha", "bravo", "charlie"] The array length is: 3 --- Start For --- 0 : alpha 1 : bravo 2 : charlie --- End For --- --- Start For-in --- 0 : alpha 1 : bravo 2 : charlie $family : Object name=array each : forEach() clean : function() associate : function() link : function() contains : function() extend : function() getLast : function() getRandom : function() include : function() combine : function() erase : function() empty : function() flatten : function() hexToRgb : function() rgbToHex : function() toJSON : function() --- End For-in ---
And here using an object.
JavaScript
console.log('===== Object Example =====');
// Object
var o = { 'one': 'alpha', 'Bravo': 'bravo', 'three': 'charlie' };
console.log('Creating a new Array:', o);
// For-in
console.log('--- Start For-in --- ');
for (var item in o) {
console.log(item, ':', o[item]);
}
console.log('--- End For-in --- ');
Output
===== Object Example ===== Creating a new Array: Object one=alpha Bravo=bravo three=charlie --- Start For-in --- one : alpha Bravo : bravo three : charlie --- End For-in ---
Read more about the for…in operation at MDC Core JavaScript 1.5 Guide: Object Manipulation.
Unfortunately this stupid mistake prevents us from using Mootools on this site. And we might have to reconsider the use of Mootols on more sites in the future — AND THAT SUCKS!
On page search for Mobile Safari on the iPhone
Posted by Martin in JavaScript, Other, Tip on July 30th, 2009
My older brother Joel just got home from Italy – and with him he brought an new shiny iPhone 3GS for me! So why bother getting it all the way from Italy you may ask? First of all it’s not released yet in Sweden (or maybe that’s today actually). Secondly and more important, what makes Italy (and Belgium) different from other European countries is that you can buy iPhones without any carrier restriction. You can use any carrier you want!
Anyways. I’ve been using it for two days now and I LOVE IT! Everything I got annoyed with and irritated over using my HTC Touch Diamond is gone. I must say, Windows Mobile sucks! Compared to the iPhone’s OS, WM is nothing! It’s like they come from different planets.
There is one thing that has been bothering me though. You can’t search the text on a page with Mobile Safari. When you view large pages on a relatively small screen, it’s necessary to be able to search it. Luckily there is a solution to the problem. At The iPhone Blog Rene Ritchie describes how you can to it with a simple bookmark! It sounds a bit weird, but all you need to do is to create a bookmark and replace the bookmark URL with some JavaScript (see code below). Then whenever you need to search a page, just click on the bookmark, enter your search text into the JavaScript prompt and click OK. The script will then highlight all the matches on the page. Super neat!!
javascript:void%28s%3Dprompt%28%27Find%20text%3A%27%2C%27%27%29%29%3Bs%3D%27%28%27+s+%27%29%27%3Bx%3Dnew%20RegExp%28s%2C%27gi%27%29%3Brn%3DMath.floor%28Math.random%28%29*100%29%3Brid%3D%27z%27%20+%20rn%3Bb%20%3D%20document.body.innerHTML%3Bb%3Db.replace%28x%2C%27%3Cspan%20name%3D%27%20+%20rid%20+%20%27%20id%3D%27%20+%20rid%20+%20%27%20style%3D%5C%27color%3A%23000%3Bbackground-color%3Ayellow%3B%20font-weight%3Abold%3B%5C%27%3E%241%3C/span%3E%27%29%3Bvoid%28document.body.innerHTML%3Db%29%3Balert%28%27Found%20%27%20+%20document.getElementsByName%28rid%29.length%20+%20%27%20matches.%27%29%3Bwindow.scrollTo%280%2Cdocument.getElementsByName%28rid%29%5B0%5D.offsetTop%29%3B
Keetology’s Mootools articles
Posted by Martin in JavaScript, Links, Mootools on July 21st, 2009
Mark Obcena has started writing a series of Mootools articles for the intermediate Mootooler at he’s blog Keetology. So far he has posted two really, really good articles. Check them out!
The DOM Fetcher Functions
Natives, Generics and Extending the Language
QuickBox – a quick Lightbox plugin for Mootools
Posted by Martin in Links, Web development on June 22nd, 2009
Andrew Plummer has made a new Lightbox plug-in for Mootools version 1.2 called QuickBox. It aims to be lightweight and fast and has support for scrolling thru the images using the mouse wheel.
Private Variables in Mootools
Posted by Martin in JavaScript, Links, Mootools, Web development on June 18th, 2009
Sean at mcarthurgfx.com has figured out a neat way to get support for private variables in a Mootools Class. Check out his post Getting Private Variables in a Mootools Class for download and to get all the details.
Is Google wave the future of e-mailing?
Posted by Martin in Links, News, Web development on June 2nd, 2009
Well, I don’t know. But it serenely looks promising!
Check out the Google Wave presentation from Google I/O 2009.