cant display generated html in browser (perl script)

Discussion in 'Perl Misc' started by Piet L., Mar 4, 2005.

  1. Piet L.

    Piet L. Guest

    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
     
    Piet L., Mar 4, 2005
    #1
    1. Advertising

  2. Piet L. wrote:

    > 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.
     
    Brian McCauley, Mar 4, 2005
    #2
    1. Advertising

  3. Piet L.

    Piet L. Guest

    > 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?
     
    Piet L., Mar 8, 2005
    #3
  4. Piet L. wrote:

    >>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?


    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"' ) );
     
    Brian McCauley, Mar 9, 2005
    #4
  5. Piet L.

    Piet L. Guest

    Brian McCauley <> wrote in message news:<d0mshq$188$>...
    > Piet L. wrote:
    >
    > >>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?

    >
    > 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
     
    Piet L., Mar 10, 2005
    #5
  6. Piet L. wrote:

    > Brian McCauley <> wrote in message news:<d0mshq$188$>...
    >
    >>Piet L. wrote:
    >>
    >>
    >>>>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?

    >>
    >>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.

    >
    > 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.
     
    Brian McCauley, Mar 10, 2005
    #6
  7. Brian McCauley wrote:

    > 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.
     
    Brian McCauley, Mar 11, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dpackwood
    Replies:
    3
    Views:
    1,812
  2. NWx
    Replies:
    2
    Views:
    328
  3. Stu
    Replies:
    2
    Views:
    795
    Rob McAninch
    Apr 6, 2004
  4. Marek Kurowski
    Replies:
    1
    Views:
    998
    Mark A. Odell
    Aug 18, 2004
  5. Nagaraj
    Replies:
    1
    Views:
    872
    Lionel B
    Mar 1, 2007
Loading...

Share This Page