XML::Compile::Schema::XmlReader

XML::Compile::Schema::XmlReader Perl module contains bricks to translate XML to HASH.
Download

XML::Compile::Schema::XmlReader Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Mark Overmeer
  • Publisher web site:
  • http://search.cpan.org/~markov/

XML::Compile::Schema::XmlReader Tags


XML::Compile::Schema::XmlReader Description

XML::Compile::Schema::XmlReader Perl module contains bricks to translate XML to HASH. XML::Compile::Schema::XmlReader Perl module contains bricks to translate XML to HASH.SYNOPSIS my $schema = XML::Compile::Schema- >new(...); my $code = $schema- >compile(READER = > ...);The translator understands schema's, but does not encode that into actions. This module implements those actions to translate from XML into a (nested) Perl HASH structure.DETAILSProcessing WildcardsIf you want to collect information from the XML structure, which is permitted by any and anyAttribute specifications in the schema, you have to implement that yourself. The problem is XML::Compile has less knowledge than you about the possible data.anyAttributeBy default, the anyAttribute specification is ignored. When TAKE_ALL is given, all attributes which are fulfilling the name-space requirement added to the returned data-structure. As key, the absolute element name will be used, with as value the related unparsed XML element.In the current implementation, if an explicit attribute is also covered by the name-spaces permitted by the anyAttribute definition, then it will also appear in that list (and hence the handler will be called as well).Use XML::Compile::Schema::compile(anyAttribute) to write your own handler, to influence the behavior. The handler will be called for each attribute, and you must return list of pairs of derived information. When the returned is empty, the attribute data is lost. The value may be a complex structure.Example: anyAttribute in XmlReaderSay your schema looks like this: < schema targetNamespace="http://mine" xmlns:me="http://mine" ... > < element name="el" > < complexType > < attribute name="a" type="xs:int" / > < anyAttribute namespace="##targetNamespace" processContents="lax" > < / complexType > < / element > < simpleType name="non-empty" > < restriction base="xs:NCName" / > < / simpleType > < / schema >Then, in an application, you write: my $r = $schema- >compile(READER = > '{http://mine}el' , anyAttribute = > 'ALL' ); my $h = $r- >( < < '__XML' ); < el xmlns:me="http://mine" > < a >42< /a > < b type="me:non-empty" > everything < / b > < / el > __XML use Data::Dumper 'Dumper'; print Dumper $h; __XML__The output is something like $VAR1 = { a = > 42 , '{http://mine}a' = > ... # XML::LibXML::Node with < a >42< /a > , '{http://mine}b' = > ... # XML::LibXML::Node with < b >everything< /b > };You can improve the reader with a callback. When you know that the extra attribute is always of type non-empty, then you can do my $read = $schema- >compile ( READER = > '{http://mine}el' , anyAttribute = > &filter ); my $anyAttRead = $schema- >compile ( READER = > '{http://mine}non-empty' ); sub filter($$$$) { my ($fqn, $xml, $path, $translator) = @_; return () if $fqn ne '{http://mine}b'; (b = > $anyAttRead- >($xml)); } my $h = $r- >( see above ); print Dumper $h;Which will result in $VAR1 = { a = > 42 , b = > 'everything' };The filter will be called twice, but return nothing in the first case. You can implement any kind of complex processing in the filter. Requirements: · Perl


XML::Compile::Schema::XmlReader Related Software