Question about XML::Generator

Question about XML::Generator

am 20.11.2007 01:01:12 von Oliver Weichhold

In the perl script below, the $xml is printed just fine. What I need to
do is conditionally print some of the xml tags. Something like this:

if ($x = $y) {
$xml->TAG2A("CCC"),
}

Of course, you can't place an if statement within the print $xml block.
How can I conditionally print one or more of the tags below?

-Thanks



#!/usr/bin/perl

use strict;
use warnings;

use XML::Generator;

my $xml = XML::Generator->new(pretty => 2, empty => 'compact');


print $xml->TAG1(
$xml->TAG1A("AAA"),
$xml->TAG1B("BBB"),

$xml->TAG2(
$xml->TAG2A("CCC"),
$xml->TAG2B("DDD"),
),

$xml->TAG3(
$xml->TAG3A("EEE"),
$xml->TAG3B("FFF"),

));

Re: Question about XML::Generator

am 20.11.2007 03:48:27 von Ben Morrow

Quoth nospam :
> In the perl script below, the $xml is printed just fine. What I need to
> do is conditionally print some of the xml tags. Something like this:
>
> if ($x = $y) {
> $xml->TAG2A("CCC"),
> }
>
> Of course, you can't place an if statement within the print $xml block.
> How can I conditionally print one or more of the tags below?

Several ways:

> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> use XML::Generator;
>
> my $xml = XML::Generator->new(pretty => 2, empty => 'compact');
>
>
> print $xml->TAG1(
> $xml->TAG1A("AAA"),
> $xml->TAG1B("BBB"),

do {
if ($x == $y) {
$xml->TAG2(...);
}
();
},

Here the final () is necessary to ensure that the do returns the empty
list. Otherwise it returns the value of $x==$y, which will be the empty
string.

($x == $y
? $xml->TAG2(...)
: ()
),

Or, since XML::Generator will ignore an empty string (or rather, create
an empty text node, which doesn't affect the resulting XML),

($x == $y and $xml->TAG2(...)),

Ben

Re: Question about XML::Generator

am 20.11.2007 04:53:13 von Oliver Weichhold

On Tue, 20 Nov 2007 02:48:27 +0000, Ben Morrow wrote:
> Or, since XML::Generator will ignore an empty string (or rather, create
> an empty text node, which doesn't affect the resulting XML),
>
> ($x == $y and $xml->TAG2(...)),
>
> Ben


Thank you!

Re: Question about XML::Generator

am 20.11.2007 21:16:29 von hjp-usenet2

On 2007-11-20 02:48, Ben Morrow wrote:
> Quoth nospam :
>> In the perl script below, the $xml is printed just fine. What I need to
>> do is conditionally print some of the xml tags. Something like this:
>>
>> if ($x = $y) {
>> $xml->TAG2A("CCC"),
>> }
>>
>> Of course, you can't place an if statement within the print $xml block.
>> How can I conditionally print one or more of the tags below?
>
> Several ways:
[...]
>> print $xml->TAG1(
>> $xml->TAG1A("AAA"),
>> $xml->TAG1B("BBB"),
>
> do {
> if ($x == $y) {
> $xml->TAG2(...);
> }
> ();
> },
[other ways to do it inside an expression snipped]

Alternatively, you can store the tags you want to use in an array, like
this:

#!/usr/bin/perl

use strict;
use warnings;

use XML::Generator;

my $x = 0;
my $y = 1;

my $xml = XML::Generator->new(pretty => 2, empty => 'compact');

my @tag2 = ();

if ($x == $y) {
push @tag2, $xml->TAG2A("CCC");
}
push @tag2, $xml->TAG2B("DDD");


print $xml->TAG1(
$xml->TAG1A("AAA"),
$xml->TAG1B("BBB"),

$xml->TAG2(
@tag2
),

$xml->TAG3(
$xml->TAG3A("EEE"),
$xml->TAG3B("FFF"),

));
__END__

In this simple example doing it inline is arguably more elegant. In more
complicated cases using variables may be more readable, especially if
they have more descriptive names than "@tag2".

hp


--
_ | Peter J. Holzer | It took a genius to create [TeX],
|_|_) | Sysadmin WSR | and it takes a genius to maintain it.
| | | hjp@hjp.at | That's not engineering, that's art.
__/ | http://www.hjp.at/ | -- David Kastrup in comp.text.tex