[Xml-compile] attribute and a child tag with the same name

Mark Overmeer mark at overmeer.net
Fri Mar 27 08:49:07 GMT 2009


* Jozef Kutej (jozef at kutej.net) [090327 07:44]:
> In this case <note> has "to" attribute and <to> child tag at the same
> time. I've tested it today and writer from:

Of course, this is a naming problem: both "to" meanings are quite
different, so a schema designer can easily avoid collissions by
picking better names.  But that doesn't help you.

The core cause of the problem, is that I want to keep the perl data
structure as simple as possible.  Of course, it would have been possible
to translate each element into seperate attribute and child element
hash, but that would hassle everyone with the rather rare situation of
name collision.

A comparible situation when you have substitutionGroups where the
localname of the various choices are the same and the namespace
is used to select between the choices.

(By accident) keyRewrite rules are only applied to elements, not
attributes, you can use that fact.  With qualified elements, you
would do:
      ->compile(...,  key_rewrite => 'PREFIXED')

The element 'to' will be translated into 'prefix_to', where you
have control over the 'prefix' used (defaults to the prefix used
in the first encounter of that namespace).  The attribute will
stay 'to'.

I would advice everyone to use the XML::Compile::Cache extension,
which offers you this use of prefix also with hooks, compile(),
template() etc.  I would advice every large project to enable
PREFIXED keys.

For namespace-less output (as in your example), you can still
use explicit rewrites (again only applied to elements)
 my %trans = (to => 'to_elem');
 my $reader = $schema->compile(READER => 'note', key_rewrite => \%trans);


By the way, you use this in your script:

  my $xml_file = read_file('note.xml');
  print "perl structure> ", Dumper([$reader->($xml_file)]), "\n";

But you can also directly (and probably faster) use:

  print "perl structure> ", Dumper([$reader->('note.xml')]), "\n";

Readers and writers accept everything what is accepted by
XML::Compile::dataToXML(), amongst them filenames.
-- 
Regards,
               MarkOv

------------------------------------------------------------------------
       Mark Overmeer MSc                                MARKOV Solutions
       Mark at Overmeer.net                          solutions at overmeer.net
http://Mark.Overmeer.net                   http://solutions.overmeer.net




More information about the Xml-compile mailing list