XSLT/XML vs. JSON

From: Ally 4 Oct 2010 16:42
To: ALL1 of 24

I am experimenting with some major changes on our site at work... I'd like to combine a gradual move to ASP.NET MVC with a switch to XML/XSLT instead of straight HTML. The reasons for this are numerous, and a number are organisational rather than technical, but I digress.

 

The point is that I want to be able to create XML views. Which I can- it works fine. But the intellisense doesn't. I can either get HTML intellisense, which includes the <% %> functionality but throws up millions of validation errors (because i'm not using <html> etc), or I can get the XML intellisense, which works great but doesn't understand the <% %> stuff.

 

Anyone know of a way that I can create some custom intellisense that'll use XML formatting but also understand <% %> brackets?

EDITED: 5 Nov 2010 00:02 by ALLY
From: THERE IS NO GOD BUT (RENDLE)10 Oct 2010 16:47
To: Ally 2 of 24
What is the reason for XML/XSLT? Wouldn't it be easier to work with the MVC framework and render HTML, XML or JSON dynamically?
From: Ally12 Oct 2010 18:22
To: THERE IS NO GOD BUT (RENDLE) 3 of 24
Yes- once I take a step back I realise that this XML generation should probably be done in the controller- it just makes the code more fiddly with adding nodes, etc. rather than just typing a structure out.

The main reason to use XSLT is that we can do the transform on the client side, which is a huge bandwidth saver (cache the XSLT file, only change XML with each page). It's just a pain in implement- I'm trying to work out if it's too much of a pain.
EDITED: 12 Oct 2010 18:22 by ALLY
From: THERE IS NO GOD BUT (RENDLE)16 Oct 2010 11:27
To: Ally 4 of 24
In that case, have you considered an AJAXy site so you can just send JSON?
From: af (CAER)28 Oct 2010 09:17
To: Ally 5 of 24
I agree with Rendle. Not that I'm by any means an expert on these matters, it just seems to me a nice way to do things - send the initial page+layout as HTML, then any data from then on as JSON (which has fairly minimal overhead) which you can insert into dynamically generated markup on the client side.
EDITED: 28 Oct 2010 09:20 by CAER
From: Ally 3 Nov 2010 15:12
To: THERE IS NO GOD BUT (RENDLE) 6 of 24

The big downer for that is SEO. Though of course, XSL'd pages aren't SEO indexable either, at least .NET has all the necessary shenanigans that allow me to do the transformation server side when necessary. I could write something that would allow it to consume the AJAX content, but figure it would be a larger body of work.

 

In any case, I've figured that the more efficient way to do this will be to make an XML document in the controller, then make the view itself the XSL file (with a little fiddling).

 

Hopefully I will be able to implement this sometime in the next two years...

From: Ally 3 Nov 2010 15:13
To: af (CAER) 7 of 24
As above, but congratulations on an insanely fast forum.
From: af (CAER) 3 Nov 2010 19:41
To: Ally 8 of 24

The way I plan to do my forum, the thread links will, if Javascript is not enabled, simply link to a new page that the server will render, in effect working like a regular non-AJAX page.

 

Fortunately both the AJAXy page and the static version both get sourced from the same post template, but even so it's still duplicating a lot of stuff so it's hardly optimal.

 

And thanks! Although I suspect a lot of that speed is down to the fact there's only about 6 threads, 6 users and 100 posts :D Whether it's still fast with a DB the size of Teh's, and the traffic to match, remains to be seen.

EDITED: 3 Nov 2010 19:41 by CAER
From: Dan (HERMAND) 3 Nov 2010 20:19
To: af (CAER) 9 of 24

Thought about generating a load of database content to check it out?

 

As for server load, not that Teh is slow, but I don't think we get a great deal of traffic - I bet you could simulate it with relative ease. Matt, what's our bandwidth use / hits like?

From: af (CAER) 3 Nov 2010 20:21
To: Dan (HERMAND) 10 of 24
Yeah I already made a thing on my local copy to generate a 100-post thread so I can work on the pagination (well, it'll be more like how Twitter used to work, with a 'More' button that just loads and appends more posts).
From: Dan (HERMAND) 3 Nov 2010 20:23
To: af (CAER) 11 of 24
I can't remember what it was I used, but as you scrolled down to the bottom of the page it started loading the subsequent posts. That was nice.
From: af (CAER) 3 Nov 2010 20:26
To: Dan (HERMAND) 12 of 24
Yeah that's the way Twitter (and Google Reader) works now.
From: THERE IS NO GOD BUT (RENDLE) 4 Nov 2010 00:04
To: Ally 13 of 24

OK, if you're doing MVC, then you do SEO (and improve accessibility generally, btw) by using jQuery to do progressive enhancement. So, you have your page layouts and you can render those to traditional HTML, but you can also get your controller methods to return JSON if the client asks for that in the request header.

 

On the page load of the initial page, you run a jQuery function to find all the links on the pages and replace them with AJAX requests for the JSON data which you can then render client-side (take a look at the new Templates plug-in for that while you're at it).

 

Since SE spiders don't implement Javascript, they'll get the old-style traditional HTML, as will Readers for the visually impaired.

 

Easy.

From: af (CAER) 4 Nov 2010 09:21
To: THERE IS NO GOD BUT (RENDLE) 14 of 24
jQuery really is lovely, isn't it?
From: Ally 4 Nov 2010 20:12
To: af (CAER) 15 of 24
MOOTOOLS RULES
From: Ally 4 Nov 2010 20:14
To: THERE IS NO GOD BUT (RENDLE) 16 of 24

I had considered that. But loading entire pages through JSON results in the URL being wrong unless you use one of those systems that appends a # into the URL, no?

 

Still, I'll bear it in mind, cheers. I'm still keen on XSL simply because it means zero change of one of the design people having access to backend code through <% %> tags. But that might just be my paranoia kicking in.

From: af (CAER) 4 Nov 2010 21:00
To: Ally 17 of 24

Could you not just have the Javascript append "&format=json" to the request URLs, then let your request handler deal with things depending on the presence of that parameter?

 

edit: that's how I plan on doing it, so if it's not good way, let me know so I can avoid any problems :D

EDITED: 4 Nov 2010 21:00 by CAER
From: Ally 4 Nov 2010 21:03
To: af (CAER) 18 of 24

Oh yes, you could. But the original page URL wouldn't change because you'd be loading content via AJAX. To get around this, people end up using URLs like:

 

http://www.mysite.com/#category/page

 

and change the part after the # dynamically. It's not a big issue (Facebook gets away with it quite nicely) but it offends my OCD tendencies.

From: af (CAER) 4 Nov 2010 21:13
To: Ally 19 of 24
Ohh yeah I see what you mean - I'd worried about that actually, and how to get around the problem of dynamic page changes not affecting the browser's history state.
From: Ally 4 Nov 2010 21:17
To: af (CAER) 20 of 24
There are some JS plugins (I've used ReallySimpleHistory before) that handle history state and such.

It's still a pain, though.
EDITED: 4 Nov 2010 23:53 by ALLY