5

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!

2
  • How do you know you're looking at the source code? It's very unlikely these days that the HTML is hand-authored; it's almost certainly generated from something else, and without inside knowledge there's no way of telling how it was generated or how the original source is maintained. Having said that, it's certainly true that only a small minority of sites are using XSLT client-side. There are many reasons for that, one being that the browser vendors show little enthusiasm for XML or XSLT - they're still only offering XSLT 1.0, which is over 20 years old. Commented Aug 1, 2020 at 20:27
  • Why not have a look at SaxonJS: saxonica.com/saxon-js/index.xml ? Commented Aug 2, 2020 at 2:42

2 Answers 2

4

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.

3
  • 1
    I disagree with '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', it is simply a problem of XSLT 1 predating HTML5 and HTML5 abandoning the SGML based use of DOCTYPEs with public id and/or system id that is used in HTML 4. Whether inside a brower or outside, XSLT 1.0's xsl:output was not designed to create a simple <!DOCTYPE html> without a public and/or a system identifier. Commented Aug 2, 2020 at 8:16
  • 1
    And the recommended hack in HTML5 (w3.org/TR/2014/REC-html5-20141028/syntax.html#the-doctype) is <xsl:output method="html" doctype-system="about:legacy-compat"/>. Commented Aug 2, 2020 at 8:21
  • I may have to apologize: the website I created this way has been more than 10 years old, but the spec you referenced seems to be from 2014. However, the precious options to improve the stack have passed and nowadays the development stack is completely different. So a discussion about details is pretty useless.
    – zx485
    Commented Aug 3, 2020 at 22:10
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.