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

P

PL

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
 
J

James Willmore

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
 
B

Brian McCauley

James said:
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;




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.
 

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
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top