cant display generated html in browser (perl script)

P

Piet L.

Hey,
I've written a perl script which creates an xml file based on input
parameters.
Now I have an xslt to display the information, but the problem is that
my browser (microsoft internet explorer) can not display the info.

Here is what I use to generate the xml/xslt transformation

my $xslt = XML::XSLT->new (myxsl, warnings => 1);
$xslt->transform (myxsl);
print $xslt->toString;
##print $xslt->serve(myxml);
$xslt->dispose();


My xslt looks like

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput method="html" encoding="ISO-8859-1"/>
<xsl:template match="/">
<html><head>
<link rel="stylesheet"
href="http://www.vub.ac.be/styles/vub.css" type="text/css"/>
</head><body><div id="pageheader"><h1><span>XXX</span></h1>
</div><div id="content"><div id="content"><div
id="contenttitle"><h2><span>MY CONTENTTITLE</span></h2></div><div
id="contenttext"><xsl:for-each select="//publication"><ul><li
value="0"><h4>
<xsl:value-of select="//title_1"/></h4><xsl:value-of
select="//name"/></li></ul></xsl:for-each>
</div></div></div></body></html></xsl:template></xsl:stylesheet>


I didn't display the xml and so, because the transformation works when
I test it in an xslt editor.

The only thing that is display is the title and so,
Does my problem have to do with the xml::xslt parser? And so, how do I
solve this?

thx
 
B

Brian McCauley

Piet said:
I've written a perl script which creates an xml file based on input
parameters.

The fact that the XML was generated by Perl does not necessarily make
this a Perl question.
Now I have an xslt to display the information, but the problem is that
my browser (microsoft internet explorer) can not display the info.

Here is what I use to generate the xml/xslt transformation

my $xslt = XML::XSLT->new (myxsl, warnings => 1);
$xslt->transform (myxsl);
print $xslt->toString;
##print $xslt->serve(myxml);
$xslt->dispose();

I've never used XML::XSLT but from a quick look at the documentation it
appears that transform() should be open_xml(). Also you do now show us
the definitions of the myxsl and myxml functions.

Please produce a _minimal_ but _complete_ script.

Anyhow this is not the 'right' way to use XSL on the web. You should
simply put a processing instruction in the XML to tell the browser where
to fetch the XSLT.
 
P

Piet L.

Anyhow this is not the 'right' way to use XSL on the web. You should
simply put a processing instruction in the XML to tell the browser where
to fetch the XSLT.


How do I do that?
 
B

Brian McCauley

Piet said:
How do I do that?

The processing instruction looks something like this:

<?xml-stylesheet href="whatever.xsl" type="text/xsl"?>

How you put such a processing instruction into an XML document would
depend on the tools you are using to generate the XML document.

For example using XML::DOM to create an XML document it would look
something like this:

my $stylesheet='whatever.xml';
my $xmldoc = XML::DOM::Document->new;
$xmldoc->setXMLDecl($xmldoc->createXMLDecl('1.0','UTF-8'));
$xmldoc->appendChild
( $xmldoc->createProcessingInstruction
( 'xml-stylesheet',
qq'href="$stylesheet" type="text/xsl"' ) );
 
P

Piet L.

Brian McCauley said:
The processing instruction looks something like this:

<?xml-stylesheet href="whatever.xsl" type="text/xsl"?>

How you put such a processing instruction into an XML document would
depend on the tools you are using to generate the XML document.

For example using XML::DOM to create an XML document it would look
something like this:

my $stylesheet='whatever.xml';
my $xmldoc = XML::DOM::Document->new;
$xmldoc->setXMLDecl($xmldoc->createXMLDecl('1.0','UTF-8'));
$xmldoc->appendChild
( $xmldoc->createProcessingInstruction
( 'xml-stylesheet',
qq'href="$stylesheet" type="text/xsl"' ) );

Thank you very much for your answer.

Actually my script is like this:
# create a DBI connection
my $dbh = DBI->connect("DBI:mysql:myDB",$user,$login);

# instantiate a new XML::Handler::YAWriter object
my $handler = XML::Handler::YAWriter->new(AsString => "-",
Pretty => {PrettyWhiteNewline => 1,
PrettyWhiteIndent => 1,
CatchEmptyElement => 1,
NoProlog => 1});
my $generator = XML::Generator::DBI->new(
Handler => $handler,
dbh => $dbh,
Indent => 1
);

my $ select = qq(.....);
my $xml = $generator->execute($select);
$dbh->disconnect ();

#and then I used the XML::XSLT module to transform and display.

So I now have the following questions,
hope you can answer them.

- How do I put your answer in?
- How will it then be displayed in a browser? Do I need an extra
command in my script or so?


thanx
 
B

Brian McCauley

Piet said:
Actually my script is like this:
# create a DBI connection
my $dbh = DBI->connect("DBI:mysql:myDB",$user,$login);

# instantiate a new XML::Handler::YAWriter object
my $handler = XML::Handler::YAWriter->new(AsString => "-",
Pretty => {PrettyWhiteNewline => 1,
PrettyWhiteIndent => 1,
CatchEmptyElement => 1,
NoProlog => 1});
my $generator = XML::Generator::DBI->new(
Handler => $handler,
dbh => $dbh,
Indent => 1
);

my $ select = qq(.....);
my $xml = $generator->execute($select);
$dbh->disconnect ();
So I now have the following questions,
hope you can answer them.

No, I don't know everything.
- How do I put your answer in?

You are using SAX. Sax is a piplining system that works on a serialised
document tree one bit at a time as a series of events. You need to
insert another SAX handler between your XML::Generator::DBI and your
XML::Handler::YAWriter.

So where now you have
my $generator = XML::Generator::DBI->new(
Handler => $handler, # etc
);

You will need something like

my $filter = XML::Filter::prependProcessingInstruction->new(
Handler => $handler,
Instruction => {
Target => 'xml-stylesheet',
Data => qq(href="$stylesheet" type="text/xsl"),
},
);

my $generator = XML::Generator::DBI->new(
Handler => $filter, # etc...
);

Of course I don't suppose there really is a module called
XML::Filter::prependProcessingInstruction but I would be a little
supprised if there was no SAX module on CPAN that could serve this function.

If not you could always write your own by deriving from XML::SAX::Base
and overriding start_element to insert a processing_instruction event
into the stream before the first start_element event that follows the
start_document event.

I could write this for you, but I've never used XML-SAX and there's no
point me reading the manuals and building a test harness when I could
leave that to you.

To get you started, at a guess, and I mean a *guess*, it would look a
bit like this...

package XML::Filter::prependProcessingInstruction;
use base 'XML::SAX::Base';

sub start_element {
my $self = shift;
$self->processing_instruction($self->{Instruction})
unless $self->{Seen_start_element}++;
$self->SUPER::start_element(@_);
}

sub start_document {
my $self = shift;
delete $self->{Seen_start_element};
$self->SUPER::start_document(@_);
}

- How will it then be displayed in a browser?

Exactly the same as it would if you'd performed the XSL transformation
server-side. Give or take bugs in the XSLT engines and anything in
your XSLT that is ambiguous.
Do I need an extra command in my script or so?

You need to change the Content-type from 'text/html' to
'application/xml' or 'text/xml; charset="utf-8"'. Use the 'application'
content type if your raw XML is not intended to be human-readble and the
'text' one if you have tried to make your raw XML readable (see RFC3023
for details).

You do, of course, also need to put the XSL into a directory where it
can be fetched with a URL (preferably a relative one). Ideally your web
server should be configured to serve up documents with a .xsl extension
as content-type 'application/xslt+xml' but in practice this rarely
matters.

I don't fully understand why the PI has type="text/xsl" rather than
type="application/xslt+xml" but all this is really getting very OT for a
Perl newsgroup.
 
B

Brian McCauley

Brian said:
Ideally your web
server should be configured to serve up documents with a .xsl extension
as content-type 'application/xslt+xml' but in practice this rarely matters.

I don't fully understand why the PI has type="text/xsl" rather than
type="application/xslt+xml" but all this is really getting very OT for a
Perl newsgroup.

Just for completelness I should mention that neither text/xsl nor
application/xslt+xml appear to be registered with IANA.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,375
Members
47,021
Latest member
AleciaMcMa

Latest Threads

Top