[XML::XSLT] empty result while parsing xml file

Discussion in 'Perl Misc' started by PL, Dec 9, 2004.

  1. PL

    PL Guest

    Hey
    I'm using in my perl-script following line:
    my $xslparser = XML::XSLT->new(Source => $xslfile);
    my $result = $xslparser->serve(Source => $xmlfile);
    print $result;

    Now I'm trying to perform a <xsl:template>, but It gives an empty
    result (just <html><body/></html>)
    If I try the xls-transformation with altova xmlspy it works
    correctly.


    Here is the code:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="Faculty/select">
    <html><body>
    <xsl:apply-templates select="group"/>
    </body></html>
    </xsl:template>
    <xsl:template match="Vakgroep[normalize-space(team_type)='V']">
    <xsl:value-of select="name_dutch"/>
    <xsl:value-of select="team_code"/>)<br/>
    </xsl:template>
    <xsl:template match="group"/>
    </xsl:stylesheet>


    The xmlfile looks as follows:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <Faculty><select><group>
    <team_code>DWIS</team_code>
    <name_dutch>Wiskunde</name_dutch>
    <team_type>V</team_type>
    </group></select></Faculty>


    I've taken a look at cpan, and I'm thinking that the problem is that
    certain thinks are not supported in de module.

    Can someone help?
    Thanks
    PL, Dec 9, 2004
    #1
    1. Advertising

  2. On Thu, 09 Dec 2004 12:50:13 +0100, PL wrote:

    > I'm using in my perl-script following line:
    > my $xslparser = XML::XSLT->new(Source => $xslfile);
    > my $result = $xslparser->serve(Source => $xmlfile);
    > print $result;


    I would write (as complete code that works):
    #!/usr/bin/perl

    use strict;
    use warnings;

    use XML::XSLT;

    my $xmlfile = 'test.xml';
    my $xslfile = 'test.xsl';

    my $xslparser = XML::XSLT->new(Source => $xslfile);
    my $result = $xslparser->serve(Source => $xmlfile);
    print $result;

    > Now I'm trying to perform a <xsl:template>, but It gives an empty
    > result (just <html><body/></html>)
    > If I try the xls-transformation with altova xmlspy it works
    > correctly.


    I don't see how it did. OTOH, I had not used XMLSpy, so maybe it does
    some sort of "magic" when there's an error in the XSLT. Read on ...

    > Here is the code:
    >
    > <?xml version="1.0" encoding="utf-8"?>
    > <xsl:stylesheet version="1.0"
    > xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    >
    > <xsl:template match="Faculty/select">
    > <html><body>
    > <xsl:apply-templates select="group"/>
    > </body></html>
    > </xsl:template>
    > <xsl:template match="Vakgroep[normalize-space(team_type)='V']">
    > <xsl:value-of select="name_dutch"/>
    > <xsl:value-of select="team_code"/>)<br/>
    > </xsl:template>
    > <xsl:template match="group"/>
    > </xsl:stylesheet>


    Again, here's an XSL that works:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="Faculty/select">

    <html>
    <body>
    <xsl:apply-templates select="group"/>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="Vakgroep[normalize-space(team_type)='V']">
    <xsl:value-of select="name_dutch"/>
    <xsl:value-of select="team_code"/>
    <br/>
    </xsl:template>

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

    </xsl:stylesheet>

    When using <xsl:template> and performing a match or select, you have to do
    something with the values. Just using an empty element tag is not enough.
    OTOH, using <xsl:apply-templates> will produce values - because the
    default is to provide the text() value of whatever was matched or
    selected.

    Of course, this is provided to demonstrate that there is nothing wrong
    with the module.


    < ... >
    > Can someone help?


    The results I got with the above changes were:

    Content-Type: text/xml
    Content-Length: 108

    <?xml version="1.0" encoding="UTF-8"?>

    <html><body>
    DWIS
    Wiskunde
    V
    </body></html>

    Which is XML, but would (I think) cause most browsers to choke or render
    XML instead of HTML. Visit comp.text.xml to see better ways to write XSLT
    to deliver HTML content.

    HTH

    Jim
    James Willmore, Dec 9, 2004
    #2
    1. Advertising

  3. James Willmore wrote:

    > On Thu, 09 Dec 2004 12:50:13 +0100, PL wrote:
    >
    >
    >>I'm using in my perl-script following line:
    >>my $xslparser = XML::XSLT->new(Source => $xslfile);
    >>my $result = $xslparser->serve(Source => $xmlfile);
    >>print $result;

    >
    >
    > I would write (as complete code that works):
    > #!/usr/bin/perl
    >
    > use strict;
    > use warnings;
    >
    > use XML::XSLT;
    >
    > my $xmlfile = 'test.xml';
    > my $xslfile = 'test.xsl';
    >
    > my $xslparser = XML::XSLT->new(Source => $xslfile);
    > my $result = $xslparser->serve(Source => $xmlfile);
    > print $result;
    >
    >
    >>Now I'm trying to perform a <xsl:template>, but It gives an empty
    >>result (just <html><body/></html>)
    >>If I try the xls-transformation with altova xmlspy it works
    >>correctly.

    >
    >
    > I don't see how it did. OTOH, I had not used XMLSpy, so maybe it does
    > some sort of "magic" when there's an error in the XSLT.


    I don't think there's an error in the XSLT program but rather in the
    interpreter.

    > Read on ...


    > When using <xsl:template> and performing a match or select, you have to do
    > something with the values. Just using an empty element tag is not enough.


    There's nothing wrong with a having a empty template - what it does is
    ignore part of the input tree.
    Brian McCauley, Dec 14, 2004
    #3
    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. Lukas
    Replies:
    3
    Views:
    782
    spiff
    Nov 10, 2005
  2. Replies:
    1
    Views:
    652
    Crutcher Dunnavant
    Feb 26, 2006
  3. jkflens
    Replies:
    2
    Views:
    1,444
    jkflens
    May 30, 2006
  4. Michael Tan
    Replies:
    32
    Views:
    928
    Ara.T.Howard
    Jul 21, 2005
  5. anthony
    Replies:
    3
    Views:
    231
    anthony
    Nov 16, 2005
Loading...

Share This Page