[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSLT HTML Formatted output

Subject: RE: XSLT HTML Formatted output
From: "Scott Trenda" <Scott.Trenda@xxxxxxxx>
Date: Wed, 10 Oct 2007 09:00:26 -0500
RE:  XSLT HTML Formatted output
Adam -

I use PHP/XSLT for a lot of my personal projects. E-mail me directly for
an XSLT wrapper function that'll save you a lot of typing when setting
up your transforms.


HTML output doesn't indent the code, because that introduces whitespace
nodes into the result document, which are NOT stripped when the page is
rendered. If, for example, you had a <pre> element all formatted in your
XSLT, you wouldn't want the transform to destroy it. Likewise, if you
had a run of formatted <span> tags all next to one another with no
whitespace in between, you wouldn't want the transform to break those
apart with whitespace, because they would have spaces in between them in
the rendered HTML document. These are things that <output
method="html"/> knows about already, and handles automatically for you.

In general, with XSLT stylesheets outputting HTML, the idea is that you
format your HTML structure so you can read it in the stylesheet, and do
all of your design there. After that, you can trust the XSLT processor
to take care of the creation of the HTML itself, and if you need to make
adjustments, you alter it from the stylesheet level. You should only be
looking at the final source if you really need to nose out an error that
isn't apparent from the XSLT stylesheet.

And if you do need to indent the source for one reason or another,
debugging or whatnot, just copy-paste the source into Notepad++, use the
HTML Tidy extension to create proper XML from it, and use the XML Tools
plugin to pretty-print (indent) it. Pretty simple once you get the hang
of it. ^_^


~ Scott


-----Original Message-----
From: adam@xxxxxxxxxxxxxxxxx [mailto:adam@xxxxxxxxxxxxxxxxx]
Sent: Wednesday, October 10, 2007 3:43 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject:  XSLT HTML Formatted output

Can anyone tell me why I cant get the output from my XSLT test to output
with the HTML indented?

I have tried the indent option but the html still comes out with all the
intents stripped out.

I am a complete newbe with all of this, I am trying to make the swich
from
Smarty template engine to XSLT.

Here is what i am doing.

Firstly I have a php script that pulls the XML file and XSL files
together:
---
<?php

$xsl_filename = "styles.xsl";
$xml_filename = "data.xml";

$doc = new DOMDocument();
$xsl = new XSLTProcessor();

$doc->load($xsl_filename);
$xsl->importStyleSheet($doc);

$doc->load($xml_filename);
echo $xsl->transformToXML($doc);

?>
---

This is my XML test data:
---
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
        <title>Welcome to this site</title>
        <catalog>
                <cd>
                        <id>1</id>
                        <title>Empire Burlesque</title>
                        <artist>Bob Dylan</artist>
                        <country>USA</country>
                        <company>Columbia</company>
                        <price>10.9000</price>
                        <year>1985</year>
                </cd>
                <cd>
                        <id>2</id>
                        <title>Hide your heart</title>
                        <artist>Bonnie Tyler</artist>
                        <country>UK</country>
                        <company>CBS Records</company>
                        <price>9.9000</price>
                        <year>1988</year>
                </cd>
                <cd>
                        <id>3</id>
                        <title>Greatest Hits</title>
                        <artist>Dolly Parton</artist>
                        <country>USA</country>
                        <company>RCA</company>
                        <price>9.9000</price>
                        <year>1982</year>
                </cd>
                <cd>
                        <id>4</id>
                        <title>Still got the blues</title>
                        <artist>Gary Moore</artist>
                        <country>UK</country>
                        <company>Virgin records</company>
                        <price>10.2000</price>
                        <year>1990</year>
                </cd>
                <cd>
                        <id>5</id>
                        <title>Eros</title>
                        <artist>Eros Ramazzotti</artist>
                        <country>EU</country>
                        <company>BMG</company>
                        <price>9.9000</price>
                        <year>1997</year>
                </cd>
                <cd>
                        <id>6</id>
                        <title>One night only</title>
                        <artist>Bee Gees</artist>
                        <country>UK</country>
                        <company>Polydor</company>
                        <price>10.9000</price>
                        <year>1998</year>
                </cd>
                <cd>
                        <id>7</id>
                        <title>Sylvias Mother</title>
                        <artist>Dr.Hook</artist>
                        <country>UK</country>
                        <company>CBS</company>
                        <price>8.1000</price>
                        <year>1973</year>
                </cd>
                <cd>
                        <id>8</id>
                        <title>Maggie May</title>
                        <artist>Rod Stewart</artist>
                        <country>UK</country>
                        <company>Pickwick</company>
                        <price>8.5000</price>
                        <year>1990</year>
                </cd>
                <cd>
                        <id>9</id>
                        <title>Romanza</title>
                        <artist>Andrea Bocelli</artist>
                        <country>EU</country>
                        <company>Polydor</company>
                        <price>10.8000</price>
                        <year>1996</year>
                </cd>
                <cd>
                        <id>10</id>
                        <title>When a man loves a woman</title>
                        <artist>Percy Sledge</artist>
                        <country>USA</country>
                        <company>Atlantic</company>
                        <price>8.7000</price>
                        <year>1987</year>
                </cd>
                <cd>
                        <id>11</id>
                        <title>Black angel</title>
                        <artist>Savage Rose</artist>
                        <country>EU</country>
                        <company>Mega</company>
                        <price>10.9000</price>
                        <year>1995</year>
                </cd>
                <cd>
                        <id>12</id>
                        <title>1999 Grammy Nominees</title>
                        <artist>Many</artist>
                        <country>USA</country>
                        <company>Grammy</company>
                        <price>10.2000</price>
                        <year>1999</year>
                </cd>
                <cd>
                        <id>13</id>
                        <title>For the good times</title>
                        <artist>Kenny Rogers</artist>
                        <country>UK</country>
                        <company>Mucik Master</company>
                        <price>8.7000</price>
                        <year>1995</year>
                </cd>
                <cd>
                        <id>14</id>
                        <title>Big Willie style</title>
                        <artist>Will Smith</artist>
                        <country>USA</country>
                        <company>Columbia</company>
                        <price>9.9000</price>
                        <year>1997</year>
                </cd>
                <cd>
                        <id>15</id>
                        <title>Tupelo Honey</title>
                        <artist>Van Morrison</artist>
                        <country>UK</country>
                        <company>Polydor</company>
                        <price>8.2000</price>
                        <year>1971</year>
                </cd>
                <cd>
                        <id>16</id>
                        <title>Soulsville</title>
                        <artist>Jorn Hoel</artist>
                        <country>Norway</country>
                        <company>WEA</company>
                        <price>7.9000</price>
                        <year>1996</year>
                </cd>
        </catalog>
</root>
---

This is my XSL data:
---
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output indent="yes"/>

<xsl:template match="/root">
        <html>
                <head>
                        <title><xsl:apply-templates
select="title"/></title>
                </head>
                <body>
                        <h2>My CD Collection</h2>
                        <table border="1">
                                <tr bgcolor="#9acd32">
                                        <th>Title</th>
                                        <th>Artist</th>
                                </tr>
                                <xsl:for-each select="catalog/cd">
                                <xsl:sort select="price" />
                                <tr>
                                        <td><xsl:value-of select="title"
/></td>
                                        <xsl:choose>
                                        <xsl:when test="price > 10">
                                        <td
bgcolor="#ff00ff"><xsl:value-of
select="artist"/></td>
                                        </xsl:when>
                                        <xsl:when test="price > 9">
                                        <td
bgcolor="#cccccc"><xsl:value-of
select="artist"/></td>
                                        </xsl:when>
                                        <xsl:otherwise>
                                        <td><xsl:value-of
select="artist"/></td>
                                        </xsl:otherwise>
                                        </xsl:choose>
                                </tr>
                                </xsl:for-each>
                        </table>
                </body>
        </html>
</xsl:template>

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

</xsl:stylesheet>
---

And this is the source of the result (not indented??):
---
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>:: Welcome to this site ::</title>
</head>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th>Title</th>
<th>Artist</th>
</tr>
<tr>
<td>Still got the blues</td>
<td bgcolor="#ff00ff">Gary Moore</td>
</tr>
<tr>
<td>1999 Grammy Nominees</td>
<td bgcolor="#ff00ff">Many</td>
</tr>
<tr>
<td>Romanza</td>
<td bgcolor="#ff00ff">Andrea Bocelli</td>
</tr>
<tr>
<td>Empire Burlesque</td>
<td bgcolor="#ff00ff">Bob Dylan</td>
</tr>
<tr>
<td>One night only</td>
<td bgcolor="#ff00ff">Bee Gees</td>
</tr>
<tr>
<td>Black angel</td>
<td bgcolor="#ff00ff">Savage Rose</td>
</tr>
<tr>
<td>Soulsville</td>
<td>Jorn Hoel</td>
</tr>
<tr>
<td>Sylvias Mother</td>
<td>Dr.Hook</td>
</tr>
<tr>
<td>Tupelo Honey</td>
<td>Van Morrison</td>
</tr>
<tr>
<td>Maggie May</td>
<td>Rod Stewart</td>
</tr>
<tr>
<td>When a man loves a woman</td>
<td>Percy Sledge</td>
</tr>
<tr>
<td>For the good times</td>
<td>Kenny Rogers</td>
</tr>
<tr>
<td>Hide your heart</td>
<td bgcolor="#cccccc">Bonnie Tyler</td>
</tr>
<tr>
<td>Greatest Hits</td>
<td bgcolor="#cccccc">Dolly Parton</td>
</tr>
<tr>
<td>Eros</td>
<td bgcolor="#cccccc">Eros Ramazzotti</td>
</tr>
<tr>
<td>Big Willie style</td>
<td bgcolor="#cccccc">Will Smith</td>
</tr>
</table>
</body>
</html>
---

Can anyone see if i am doing something wrong?

Cheers,
Adam

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.