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

Hi Monks,

i use XML::LibXML for several years. Actually, i wanted to read an xml file containing some xinclude statements. I expected XML::LibXML to perform the include, but it doesn't. Example:

XML file foo.xml:

<?xml version="1.0"?> <foo> <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="bar.xml"/> </foo>

XML file bar.xml:

<?xml version="1.0"?> <bar> This is bar.xml! </bar>

Perl code:

use strict; use warnings; use XML::LibXML; my $dom = XML::LibXML->load_xml(location => "foo.xml"); print $dom->toString(1);

The above perl script prints:

<?xml version="1.0"?> <foo> <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="bar.xml"/ +> </foo>

Obviously, the <xi:include.../> has not been executed. Is there a way to tell XML::LibXML to do an include? Or is there another XML module that does the job?

  • Comment on Can i mange XML::LibXML to do an include? Or is there another XML module that does it?
  • Select or Download Code

Replies are listed 'Best First'.
Re: Can i mange XML::LibXML to do an include? Or is there another XML module that does it?
by haukex (Archbishop) on Mar 29, 2019 at 10:21 UTC

    See the documentation of XML::LibXML::Parser:

    use warnings; use strict; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_file("foo.xml"); $parser->processXIncludes( $doc ); print $doc->toString(1); __END__ <?xml version="1.0"?> <foo> <bar> This is bar.xml! </bar> </foo>

    Update: Alternatively, my $doc = XML::LibXML->load_xml( expand_xinclude => 1, location => "foo.xml" );