Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   [XML::XSLT] empty result while parsing xml file (http://www.velocityreviews.com/forums/t889579-xml-xslt-empty-result-while-parsing-xml-file.html)

PL 12-09-2004 11:50 AM

[XML::XSLT] empty result while parsing xml file
 
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





James Willmore 12-09-2004 01:34 PM

Re: [XML::XSLT] empty result while parsing xml file
 
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



Brian McCauley 12-14-2004 10:24 PM

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


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.



All times are GMT. The time now is 11:51 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57