Skip Menu |

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

Report information
The Basics
Id: 59476
Status: rejected
Priority: 0/
Queue: XML-Writer

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

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



Subject: DATA_INDENT for character Data
First of all thank you very much for that simple, but extremely useful Module XML::Writer (in fact, XML::Writer inspired me to name my own module XML::Reader). However, I would like to draw to your attention a small inconvenience in XML::Writer. I have a small program that exhibits the inconvenience and I propose a simple patch to resolve that inconvenience. Is there any chance that my patch could be incorporated into XML::Writer ? -- Klaus Here is the small test program: ====== use XML::Writer; my $writer = XML::Writer->new(OUTPUT => \*STDOUT, DATA_MODE => 1, DATA_INDENT => 2, UNSAFE => 1); $writer->xmlDecl("utf-8"); $writer->startTag("greeting", "class" => "simple"); $writer->characters("Hello, world!"); $writer->startTag("level-01"); $writer->startTag("level-02"); $writer->startTag("level-03"); $writer->characters("This is one text..."); $writer->characters("second text..."); $writer->startTag("level-04"); $writer->startTag("level-05"); $writer->characters("This is another text..."); $writer->endTag("level-05"); $writer->characters("Yet another text..."); $writer->endTag("level-04"); $writer->endTag("level-03"); $writer->endTag("level-02"); $writer->endTag("level-01"); $writer->endTag("greeting"); $writer->end(); ====== The output of that small program is as follows: ====== <?xml version="1.0" encoding="utf-8"?> <greeting class="simple">Hello, world! <level-01> <level-02> <level-03>This is one text...second text... <level-04> <level-05>This is another text...</level-05>Yet another text... </level-04> </level-03> </level-02> </level-01> </greeting> ====== The indentation as such is perfect, however, what is inconvenient for me is that character text does not automatically appear on its own seperate line. I allowed myself to patch the file "Writer.pm" in XML::Writer in the following way -- look for the markers # begin by Klaus Eichner: ====== [...] my $endTag = sub { my $name = $_[0]; my $currentName = pop @elementStack; $name = $currentName unless $name; $elementLevel--; # begin by Klaus Eichner, 17 July 2010 # if ($dataMode && $hasElement) { if ($dataMode) { # end by Klaus Eichner, 17 July 2010 $output->print("\n"); [...] [...] my $characters = sub { my $data = $_[0]; if ($data =~ /[\&\<\>]/) { $data =~ s/\&/\&amp\;/g; $data =~ s/\</\&lt\;/g; $data =~ s/\>/\&gt\;/g; } &{$escapeEncoding}($data); # begin by Klaus Eichner, 17 July 2010 if ($dataMode) { $output->print("\n"); $output->print(" " x ($elementLevel * $dataIndent)); } # end by Klaus Eichner, 17 July 2010 $output->print($data); [...] ====== Now the output of that small test program looks as follows (with character text automatically placed on its own seperate line). ====== <?xml version="1.0" encoding="utf-8"?> <greeting class="simple"> Hello, world! <level-01> <level-02> <level-03> This is one text... second text... <level-04> <level-05> This is another text... </level-05> Yet another text... </level-04> </level-03> </level-02> </level-01> </greeting> ======
I can see that this formatting would be better for your document. However, in general, adding newlines to text elements could change the behaviour (e.g., processors pass white space through as part of the content). At the very least, this would need to be enabled by a flag for backwards compatibility. examples/directory-as-atom.pl demonstrates setting and clearing data mode to achieve specific formatting; I hope that helps you to get the formatting you need.
Subject: DATA_INDENT for character Data (with MIXED_CONTENT for backwards compatibility)
Le Jeu 12 Aoû 2010 07:50:44, JOSEPHW a écrit : Show quoted text
> I can see that this formatting would be better for your document. > However, in general, adding newlines to text elements could change the > behaviour (e.g., processors pass white space through as part of the > content). > > At the very least, this would need to be enabled by a flag for backwards > compatibility.
I have added a flag MIXED_CONTENT => 1 for backwards compatibility. Here are my suggested changes in Writer.pm ============================ my $dataMode = $params{DATA_MODE}; my $dataIndent = $params{DATA_INDENT} || 0; # begin by Klaus Eichner, 17 July 2010 my $mixedContent = $params{MIXED_CONTENT}; # end by Klaus Eichner, 17 July 2010 # If the NEWLINES parameter is specified, # set the $nl variable appropriately my $nl = ''; [...] my $endTag = sub { my $name = $_[0]; my $currentName = pop @elementStack; $name = $currentName unless $name; $elementLevel--; # begin by Klaus Eichner, 17 July 2010 if ($dataMode && $hasElement or $dataMode && $mixedContent) { # end by Klaus Eichner, 17 July 2010 $output->print("\n"); $output->print(" " x ($elementLevel * $dataIndent)); } [...] my $characters = sub { my $data = $_[0]; if ($data =~ /[\&\<\>]/) { $data =~ s/\&/\&amp\;/g; $data =~ s/\</\&lt\;/g; $data =~ s/\>/\&gt\;/g; } &{$escapeEncoding}($data); # begin by Klaus Eichner, 17 July 2010 if ($dataMode && $mixedContent) { $output->print("\n"); $output->print(" " x ($elementLevel * $dataIndent)); } # end by Klaus Eichner, 17 July 2010 $output->print($data); ============================ Show quoted text
> examples/directory-as-atom.pl demonstrates setting and clearing data > mode to achieve specific formatting; I hope that helps you to get the > formatting you need.
I have run examples/directory-as-atom.pl, and I can see how I could achieve my specific formatting that way, but this would be complicated from the viewpoint of the user of XML::Writer. I prefer having a simple switch MIXED_CONTENT => 1. Is there any chance to merge my suggested changes into XML/Writer.pm ?
For now, I'm not going to apply this. It seems like a style of formatting that doesn't necessarily justify its own flag, and which isn't too hard to achieve with manual newlines. Thanks for the patch - I've applied it to a 'contrib' branch in the git repository.