Gregory
Gregory

Reputation: 187

Can you build a Website completely with XML and XSLT?

I've been wondering because whenever I look at a webpage's source-code it's mostly HTML. Well, could I build a Website completely with XML and XSLT as the Frontend? And are there any popular sites that use XML and XSLT as Frontend because I've not seen anything likely. I`m very curious because I like XML than HTML!

Upvotes: 5

Views: 1885

Answers (2)

zx485
zx485

Reputation: 29042

Yes. But it is a sad story.

You can associate an XML file with an XSLT-1.0 file to create the desired output browser-side with the following instruction:

<?xml-stylesheet type="text/xsl" href="transform.xslt"?>

This would transfer the load of compling the HTML to the client which had to assemble the HTML by itself, client-side. Today, this is still working in the browser.

The advantage of this approach is that you can change the data (XML file) without changing the algorithm of its representation (the HTML produced by the XSLT, styled by CSS). Very efficient, and highly customizable, because the XSLT could define different transformation rules(templates) to handle different values or sets of data without reloading the XSLT.

Theoretically, the XML could be dynamically updated via AJAX/JavaScript to change the HTML. And this is possible, but not in the best way, because you have to call the browser's XSLT processor manually to update the website with JavaScript. There is no native way to do this without using JS.

Another "problem" of this approach is creating the HTML5 <!DOCTYPE html> at the beginning of a HTML5 file. With the current browsers, this requires a hack to make it work. You have to (ab)use the xsl:output functionality in a very specific way:

<xsl:output method="html" encoding="utf-8" omit-xml-declaration="yes" doctype-system="" doctype-public="" media-type="text/html"/>

Only then the output of the XML+XSLT tranformation will be recognized as HTML5 markup.

The reason why I introduced this posting as sad news is that the whole scenario could have been far better solved if

  • there would be XSLT-3.0 support in the browsers instead of only legacy XSLT-1.0 support
  • there would be a way to better handle the HTML5 DOCTYPE
  • HTML5 would instead be XHTML5
  • the URL would be passed as a parameter to the XSLT natively. At best, with all & delimited parameters separated

Instead of making this approach easier for developers, the opposite has happened, and the combination of XML+XSLT has been further restricted for "security reasons" in the main browsers - which causes many people problems.

Upvotes: 4

kjhughes
kjhughes

Reputation: 111696

Yes, XSLT can be (and has been) used both on the server and in the browser to transform XML into HTML.

Pros

  • XML to (X)HTML transformations are natural and elegant.

Cons

  • Few devs are skilled enough in XSLT to appreciate or be proficient with the approach.
  • Browser support has waned, never progressing beyond XSLT 1.0.
  • Frameworks and libraries have emerged to create HTML more conventionally using more familiar procedural and functional language techniques.

See also

Upvotes: 1

Related Questions