Writing void tags in HTML5

alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

Writing void tags in HTML5

Post by alyxandr »

Hi all,

I'm trying to generate HTML5 (oXygen Developer 13.2, Saxon-EE 9.3.0.5), and my transform is turning void tags into end-tag style, which breaks things like <link/> and <meta/>. Is there an elegant way to get it to stop doing that, or am i stuck with something like

Code: Select all


<xsl:text disable-output-escaping='yes'>&#xa;<link rel="stylesheet" type="text/css" href="</xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping='yes'>" /></xsl:text>
to hide these tags from whatever postprocessing is happening?

(In case this is relevant, i'm using result-document method="xhtml", and the

Code: Select all

<xsl:text disable-output-escaping='yes'><!DOCTYPE html>&#xa;</xsl:text>
trick to get the doctype right.)

TIA,

--alex.
alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

Re: Writing void tags in HTML5

Post by alyxandr »

OK, looks like the easiest way to solve this is xsl:result-document method="xml", which turns off Trying-To-Be-Helpful Mode and gives you what you'd expect. So, this works:

Code: Select all


<xsl:template match="page">
<xsl:result-document method="xml" href="{@href}" omit-xml-declaration="yes" indent="yes">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html>&#xa;</xsl:text>
<html> and so on... </html>
</xsl:result-document>
</xsl:template>

<xsl:template match="stylesheet-file>
<link rel="stylesheet" type="text/css" href="{.}" />
</xsl:template>

<xsl:template match="script-file>
<script src="{.}"><xsl:text>&#160;</xsl:text></script>
</xsl:template>
...and i'm a happy boy.
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Writing void tags in HTML5

Post by Radu »

Hi Alex,

I think that your first approach was the correct one.
But in the stylesheet you should have set the namespace on the parent <html> element like:

Code: Select all

<html xmlns="http://www.w3.org/1999/xhtml">.....
You can also add this additional attribute on the <xsl:stylesheet>:

xpath-default-namespace="http://www.w3.org/1999/xhtml"

in order to keep XPath expressions in the stylesheet unprefixed and simple.

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

Re: Writing void tags in HTML5

Post by alyxandr »

Tried that, thanks, but made no difference -- still resulted in stray end tags for <link> and <meta>. Fortunately, method="xml" is working fine.

Is there any documentation around on the ugly details of method="xhtml"? My XSLT books are pretty reticent on the subject, and it's always seemed like a sort of mysterious black box to me. --alex.
Radu
Posts: 9018
Joined: Fri Jul 09, 2004 5:18 pm

Re: Writing void tags in HTML5

Post by Radu »

Hi Alex,

The stylesheet I tested with was like this:

Code: Select all

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0">
<xsl:template match="/">
<xsl:result-document method="xhtml" href="test.html" omit-xml-declaration="yes" indent="yes">
<xsl:text disable-output-escaping='yes'><!DOCTYPE html>&#xa;</xsl:text>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta content="abc"/>
<link rel="stylesheet" type="text/css" href="a.b" />
</head>
<body>
<br/>
</body>
</html>
</xsl:result-document>
</xsl:template>
</xsl:stylesheet>
and it produced in the result document the empty <meta> and <link> tags in the correct collapsed form.

I think that it is best for you to make small stylesheet examples and see how the behavior changes when the output method gets changed or when the HTML elements which are output do not have a namespace set to them.

Basically when the XSLT processor outputs in XHTML mode it should respect the XHTML compatibility guidelines:

http://www.w3.org/TR/xhtml1/guidelines.html

Regards,
Radu
Radu Coravu
<oXygen/> XML Editor
http://www.oxygenxml.com
alyxandr
Posts: 18
Joined: Sat Aug 01, 2009 7:52 pm

Re: Writing void tags in HTML5

Post by alyxandr »

OK, my namespaces weren't propagating through template calls correctly, so i was getting a xmlns="" in my head tag that was messing things up. Thanks much for the help. --alex.
Post Reply