Your solution doesn't scale. Suppose you've decided to use the first solution

(case c="(qd)whichfile(/qd)") <file1><insertfile>.... <file2... (/case)
and then later you need to enclose this in yet another case statement. OK, you can go and change all those <> to square brackets and normal brackets to <>. And what do you do next time? If the two case statements end up in yet another or in an if?

I think you need to find a different way to do this. Even if it means that the parser has to know these few statements as <if>, <case> and <for>.

Or maybe it would be best to parse the file as an ordinary XML and then process it FROM THE ROOT up, always parsing and processing the values of all attributes the same way the whole file is and then if there is a plugin specified for the tag pass both the already processed attributes and not processed content to the plugin and let it decide whether to call the processing for the subtags and then do something with the results or do something with the subtags and then call the recursive processing for the subtags. If there is no plugin, then all the subtags will be processed in order.

I've actually tried to write such a beast ... and found out that I can't write <tag attr="xxx <othertag/> yyy"/> so I decided to change < to { and > to } within the attributes --> <tag attr="xxx {othertag/} yyy"/>. Here's the result:

use strict; use XML::Rules; # at least 0.20 ! -- I just uploaded that version! my $parser = XML::Rules->new( rules => [ _default => 'raw extended arr +ay']); my %plugins; %plugins = ( set => sub { die "The <set> plugin requires a name attribute!\n" unless exi +sts($_[0]->{name}); my $content = $_[0]->{_content}; $plugins{$_[0]->{name}} = sub {$content}; return; }, print => sub { print $parser->ToXML( '', process($_[0]->{_content})); return; }, select => sub { die "The <select> plugin requires a switch attribute!\n" unles +s exists($_[0]->{switch}); my $switch = $_[0]->{switch}; if (exists $_[0]->{':' . $switch}) { # exists a subtag with th +at name return process($_[0]->{':' . $switch}[0]{_content}) } elsif (exists $_[0]->{':case'}) { # maybe there is a <case i +d="that name"> foreach my $case (@{$_[0]->{':case'}}) { return process($case->{_content}) if $case->{id} eq $s +witch; } return; } else { return; # the case was not found } }, ); my $data = $parser->parse( \*DATA); $data = $data->{_content}; #use Data::Dumper; #print Dumper($data); #print Dumper(process($data)); my $result = $parser->ToXML(@{(process($data))->[0]}); print "\n-----------------------------\n\n$result"; sub process { my ($what) = @_; if (! ref $what) { return $what } elsif (ref($what) eq 'ARRAY') { return [ map { if (!ref $_) { $_ } elsif (ref($_) eq 'ARRAY' and @$_ == 2) { processAttr($_->[1]); if (exists $plugins{$_->[0]}) { $plugins{$_->[0]}->($_->[1]) } else { $_->[1]{_content} = process($_->[1]{_content}); [ $_->[0] => $_->[1]] } } } @$what] } else { die "process() only accepts strings and array refs.\n"; } } sub processAttr { my ($attrs) = @_; foreach my $attr (keys %$attrs) { next if $attr =~ /^:/; next unless $attrs->{$attr} =~ /\{/; #print "$attrs->{$attr} -> "; $attrs->{$attr} =~ tr/{}/<>/; my $data = $parser->parse( '<root>' . $attrs->{$attr} . '</roo +t>'); $data = $data->{_content}[0][1]{_content}; #print "(" . Dumper($data) . ")"; $attrs->{$attr} = $parser->ToXML('', process($data)); #print "$attrs->{$attr}\n"; } } __END__ <html> <set name="foo">blah blah</set> <set name="bar">two</set> <set name="x">3</set> <h1>I say <foo/>, you know.</h1> <print>This is printed always!</print> <p> <select switch="{bar/}"> <one>This is one.</one> <two>This is the other.</two> <three>And this is yet another.</three> <print>This is NOT printed!</print> </select> </p> <p> <select switch="{x/}0"> <case id="10">This is ten.</case> <case id="20">This is twenty.</case> <case id="30">This is thirty.</case> <case id="41">This is fourty one.<print>This is not printed neither +!</print></case> </select> </p> </html>

The plugins get as their only parameter a reference to a hash containing the attributes, subtags and content of the tag. There are three kinds of keys in the hash:

Not sure it will work for you, but it was an interesting exercise.


In reply to Re: more bracket chaos by Jenda
in thread more bracket chaos by simonodell

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.