Skip Menu |

This queue is for tickets about the XML-Rules CPAN distribution.

Report information
The Basics
Id: 70027
Status: rejected
Priority: 0/
Queue: XML-Rules

People
Owner: Nobody in particular
Requestors: DOUGW [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Normal
Broken in: 1.10
Fixed in: (no value)



Subject: stripspaces not working correctly
The attached file should have no content for the root node (if I understand stripspaces correctly). Even if there was content, it should not be a "1" for every record parsed and should be whitespace.
Subject: tst.pl
#!/usr/bin/perl use strict; use warnings; use XML::Rules; use Data::Dumper qw(Dumper); my $xml = <<XML; <root> <record> <a>a1</a> <b>b1</b> <c>c1</c> </record> <record> <a>a2</a> <b>b2</b> <c>c2</c> </record> </root> XML my @rules = ( _default => sub {$_[0] => $_[1]->{_content}}, record => sub { print Dumper $_[1]; my $r = $_[1]; my $str = join("|", @$r{qw(a b c)}); print "$str\n"; }, root => sub { print Dumper $_[1] }, ); my $xr = XML::Rules->new( rules => \@rules, stripspaces => 3|8 ); $xr->parse($xml); __END__ $VAR1 = { 'c' => 'c1', 'a' => 'a1', 'b' => 'b1' }; a1|b1|c1 $VAR1 = { 'c' => 'c2', 'a' => 'a2', 'b' => 'b2' }; a2|b2|c2 $VAR1 = { '_content' => '11' };
The problem is not with the stripspaces, but rather with your record handler. A subroutine returns the value of the last evaluated expression, which in case of the subroutine assigned to handle <record> tags means the result of print "$str\n"; which is 1. And (exactly according to the docs) if a rule returns a single scalar then the stringified value of the scalar is inserted into the parent tag's _content; Change the rule to record => sub { print Dumper $_[1]; my $r = $_[1]; my $str = join("|", @$r{qw(a b c)}); print "$str\n"; return; }, and the 1s will go away :-)