Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transforming XML into HTML (as opposed to xhtml)

Tags:

html

xml

xslt

html4

i want to transform some xml into HTML that has the following format:

<TR><TD> col1 <TD> col2 <TD> col3 </TR>

Note: The output is HTML, complete with optional closing tags omitted. This is the problem, and the reason the question exists.

A snippet of the XSL i'm using is:

<?xml version='1.0'?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output 
    doctype-system='http://www.w3.org/TR/html4/strict.dtd'
    doctype-public='-//W3C//DTD HTML 4.01//EN'
    indent='yes'
    method='html'
    />
   ...
   <xsl:for-each select="/">  
      <TR><TD><xsl:value-of select="col1"/><TD><xsl:value-of select="col2"/><TD><xsl:value-of select="col3"/></TR>
   </xsl:for-each>

You can see that the guts of the XSL matches my desired HTML (wrapped for easy reading):

<TR>  <TD><xsl:value-of select="Column1"/>
      <TD><xsl:value-of select="Column2"/>
      <TD><xsl:value-of select="Column3"/> </TR>

Note: Those of you who know the error i'm getting from the XSLT: hopefully already know the answer.

When presented with my XSL (which, don't forget, is a form of xml), i get the non-well formed error:

End tag 'TR' does not match the start tag 'TD'.

This makes perfect sense. Indeed:

<TD><xsl:value-of select="Column3"/> </TR>

i do not close the TD element before closing the TR. So the question is:

How can i transform xml into HTML, given that HTML is not xml?

See also

  • HTML: Include, or exclude, optional closing tags?
  • XSLT: Transforming into non-xml content?
  • Omitting optional tags of html

Update one

It has been suggested that one could simply include the closing tags anyway, in order to make the XSL validate (shown wrapped for easy reading):

<TR>    <TD><xsl:value-of select="col1"/></TD>
        <TD><xsl:value-of select="col2"/></TD>
        <TD><xsl:value-of select="col3"/></TD>   </TR>

then, by using xsl:output method='html', the final HTML content would have the </TD> tags magically omitted. Except it doesn't work:

<TR><TD>col1</TD><TD>col2</TD><TD>col3</TD></TR>

Update two

It has been suggested that i give up, don't bother asking this question, and just include the optional closing tags. That's possible, but that's not my question. Also, the "solution" doesn't work for elements where the closing tag is forbidden, e.g.:

<BR/>

or

<BR></BR>

How would i include a <BR> element in my HTML output, given that it is forbidden in HTML to close a <BR> element.

like image 786
Ian Boyd Avatar asked Jun 30 '10 01:06

Ian Boyd


People also ask

How do I convert XML to HTML?

The standard way to transform XML data into other formats is by Extensible Stylesheet Language Transformations (XSLT). You can use the built-in XSLTRANSFORM function to convert XML documents into HTML, plain text, or different XML schemas. XSLT uses stylesheets to convert XML into other data formats.

What can be used to transform XML into HTML?

With XSLT you can transform an XML document into HTML.

Why is XHTML better than HTML?

XHTML is easy to use with other data formats, and it creates more neat code as it is stricter than HTML. Therefore, it is more compatible with most browsers, and it maintains a standard of code that can be used for various devices.


2 Answers

Here's one way to do this:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="/*">
   &lt;TR>&lt;TD><xsl:value-of select="col1"/>&lt;TD><xsl:value-of select="col2"/>&lt;TD><xsl:value-of select="col3"/>&lt;/TR>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the following XML document:

<t>
 <col1>1</col1>
 <col2>2</col2>
 <col3>3</col3>
</t>

the wanted result is correctly produced:

   <TR><TD>1<TD>2<TD>3</TR>
like image 143
Dimitre Novatchev Avatar answered Sep 21 '22 03:09

Dimitre Novatchev


I believe the simplest thing is to just accept you're going to have closing tags in the output. While they might be optional, I believe most people would agree that best practice is to include them.

Is there a reason you really don't want optional closing tags in the output?

Re Update Two

There's no problem with this update. With method="html" <BR/> will be output as <BR>:

XSLT (note <BR/>):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output 
doctype-system='http://www.w3.org/TR/html4/strict.dtd'
doctype-public='-//W3C//DTD HTML 4.01//EN'
indent='yes'
method='html'
/>

<xsl:template match="/">
<HTML><BODY>
    <TR>
        <xsl:apply-templates/>
    </TR>
    <BR/> <!-- HERE -->
</BODY></HTML>
</xsl:template>

<xsl:template match="item">
    <TD><xsl:value-of select="."/></TD>
</xsl:template>

</xsl:stylesheet>

Input:

<root>
<item>one</item>
<item>two</item>
</root>

Output (note <BR>):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<HTML><BODY>
<TR>
<TD>one</TD>
<TD>two</TD>
</TR>
<BR> <!-- HERE -->
</BODY></HTML>
like image 37
porges Avatar answered Sep 23 '22 03:09

porges