Writing void tags in HTML5

alyxandr
Posts: 18

Writing void tags in HTML5

Mon Apr 09, 2012 10:46 pm

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;&lt;link rel="stylesheet" type="text/css" href="</xsl:text>
<xsl:value-of select="."/>
<xsl:text disable-output-escaping='yes'>" /&gt;</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'>&lt;!DOCTYPE html>&#xa;</xsl:text>
trick to get the doctype right.)

TIA,

--alex.
alyxandr
Posts: 18

Re: Writing void tags in HTML5

Tue Apr 10, 2012 1:18 am

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'>&lt;!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: 5088

Re: Writing void tags in HTML5

Tue Apr 10, 2012 8:45 am

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

Re: Writing void tags in HTML5

Tue Apr 10, 2012 7:17 pm

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: 5088

Re: Writing void tags in HTML5

Wed Apr 11, 2012 9:00 am

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'>&lt;!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

Re: Writing void tags in HTML5

Thu Apr 12, 2012 8:00 pm

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.

Return to “XHTML”

Who is online

Users browsing this forum: No registered users and 0 guests