tdane has asked for the wisdom of the Perl Monks concerning the following question:

I use perl a lot for "getting the job done." I'm in a corner now where I have to use perl for a production-y tool that calls an XML REST API. Normally for perl and XML I make do with XML::Simple. I know it is discouraged, but is *is* simple and gets the job done.

Now I need to read an XML document, modify it, and POST it back to that REST API compliant with an XML Schema. XML::Simple won't cut it -- too many ways it will write the XML out differently than the input. So I have turned to XML::Compile and XML::LibXML. I've got it working, but I think it can work better for my purpose.

I am loading my schema with XML::Compile and and I am reading in my XML document with a READER. I end up with a reference variable to XML::LibXML::Element. I am able to use method calls like ->addChild to do to this structure what I need to do, but then I found this blog post which shows me how I can construct a hash of hashes not dissimilar to what XML::Simple gives me. I am much more comfortable manipulating this kind of structure. The blog post shows how to get an "empty" template, fill it out, and use that structure to generate an XML document. This is great, but for my use case I don't want a completely empty structure where I have to populate every tag. I want a structure populated with values from an input XML document.

In other words, I have done a GET from my REST API and I have an XML document in a string compliant with my Schema (which I have successfully built with XML::Compile). Now instead of using the READER to get perl objects where I have to use methods to access and change the contents, I want to get the same kind of structure as the example from the blog:

warn $schema->template('PERL', 'addresses');

But instead of an empty structure, I want it initialized with the data from my XML string. Can anyone show me the way to do that? I am just NOT finding it...

Replies are listed 'Best First'.
Re: XML::Compile template initialized from XML document
by Corion (Patriarch) on May 11, 2016 at 17:28 UTC

      I accept the possibility. I'm new to both library families (XML::Compile and XML::LibXML) and they are large and complicated. Let me show you the code I use to compile the schema and to parse the XML that produces a structure I find difficult to use, and maybe you can help me get to this easier model

      I'm trying to post the minimal helpful code. The getSASchema subroutine successfully downloads the XSD and compiles it. The saQuery subroutine successfully pulls the XML I want and runs the reader I get from the schema object's "compile" method. As you see the subroutine returns the reference that comes back from calling the reader with the XML text.

      ... ... sub getSASchema { my ($config, $lwp) = @_; my $saSchemaUrl = "https://" . $config->{saserver} . ":" . $config +->{saport} . "/serverautomation/SA-REST.xsd"; my $sareq = HTTP::Request->new( GET => $saSchemaUrl ); $sareq->authorization_basic($config->{besuser}, $config->{bespassw +ord}); my $xsd = $lwp->request($sareq); my $schema = XML::Compile::Schema->new($xsd->{_content}); return $schema; } ## Handle querying the Server Automation API. sub saQuery { my ($config, $lwp, $schema) = @_; my $saPlanUrl = "https://" . $config->{saserver} . ":" . $config->{saport} . "/serverautomation" . $config->{saplanurl}; my $sareq = HTTP::Request->new( GET => $saPlanUrl ); $sareq->authorization_basic($config->{besuser}, $config->{bespassw +ord}); my $xml = $lwp->request($sareq); my $planreader = $schema->compile( READER => "{http://iemfsa.tivol +i.ibm.com/REST}sa-rest"); my $xmltxt = $xml->{_content}; my $tree = $planreader->($xmltxt); return $tree; } ... ... # Down in my main. $config is a hash containing config # data parsed from a file, and $lwp is an instance of # LWP::UserAgent my $saSchema = getSASchema($config, $lwp); ## Fetch the "raw" automation plan from the BigFix SA server my $doc = saQuery($config, $lwp, $saSchema); my $basenode = $doc->{_}; print Dumper($doc); print Dumper($basenode);

      As you see in my main code, I then use Dumper to see what I get from that process. Here's what I get:

      $VAR1 = { '_MIXED_ELEMENT_MODE' => 'ATTRIBUTES', '_' => bless( do{\(my $o = 85686224)}, 'XML::LibXML::Element +' ) }; $VAR1 = bless( do{\(my $o = 85686224)}, 'XML::LibXML::Element' );

      That is not a hash of hashes. It is XML::LibXML objects. This is how the XML::Compile documentation tells me to make a reader, but I'm not sure this reader is the same reader you refer to as XML::Compile::Translate::Reader. Maybe you can show me a fragment to replace my reader in saQuery with one that you think will produce what I want? And please remember that after modifying my structure I need to be able to use my XML::Compile::Schema object to construct compliant output XML...

      I really appreciate your help. I'm learning a lot.

        Be sure that you create the ::Schema (better the ::Cache) only once in your program, and reuse it. The same for your compiled handlers: compilation is expensive, (re)use is cheap.

        Probably you want to use $xmltxt = $msg->decoded_content

        Apparently, your schema uses mixed="true". When that is not a mistake (which it often is), than you have to do things partially manually. XHTML is an example of mixed XML: it does not translate into a predictable DOM tree. Tune the handling of mixed elements to suit your needs via the mixed_elements parameter.

Re: XML::Compile template initialized from XML document
by markov (Scribe) on May 12, 2016 at 06:56 UTC

    "Template" in XML::Compile is not provided as "fill this in" HASH, but as example of the expected message. Schema's can be horribly complex, sometimes for much simpler actual message structures. So, template() is a helper during development, not the solution.

    What most people do, is producing the example via template(), and then write a program which smartly produces a structure like that. If you get runtime errors about your generated structure, it is easier to search for possible mistakes.