XML-LibXML-Overlay
view release on metacpan or search on metacpan
Build.PL
Changes
lib/XML/LibXML/Overlay.pm
lib/XML/LibXML/Overlay/Document.pm
Makefile.PL
MANIFEST
META.yml
README
t/XML-LibXML-Overlay.t
t/xml/overlay.xml
t/xml/target.xml
# <isbn>9780596000271</isbn>
# </book>
# <book id="book2">
# <author>Elliotte Rusty Harold</author>
# <author>W. Scott Means</author>
# <title>XML in a Nutshell: A Desktop Quick Reference</title>
# <isbn>9780596007645</isbn>
# </book>
# </catalog>
# overlay.xml
####
# <overlay>
# <target xpath="/catalog/book[@id='book0']/author[text()='Delete Me!']">
# <action type="delete" />
# </target>
# <target xpath="/catalog/book[@id='book2']">
# <action type="insertBefore">
# <book id="book2">
# <author>Mark Jason Dominus</author>
# <title>Higher-Order Perl. Transforming Programs with Programs</title>
# <isbn>9781558607019</isbn>
# </book>
# </action>
# </target>
# </overlay>
use XML::LibXML;
use XML::LibXML::Overlay;
my $overlay = XML::LibXML::Overlay->load_xml(
'location' => '/path/to/overlay.xml',
);
my $target = XML::LibXML->load_xml(
'location' => '/path/to/target.xml',
);
$overlay->apply_to($target);
# do whatever you want with $target
DESCRIPTION
XML::LibXML::Overlay allowes to apply overlay files to XML files. This
modul is a rewirte of XML::Overlay, but it uses plain XML::LibXML
instead of the Class::XML thru XML::Parser stack.
DETAILS
XML::LibXML::Overlay inherits from XML::LibXML. So you can use
XML::LibXML::Overlay like XML::LibXML. The only difference is, that
"load_xml" returns a XML::LibXML::Overlay::Document instead of a
XML::LibXML::Document.
Tags
Following Tags can be used in a overlay document.
overlay
Specifies the root element, and contains any target element.
target
Selectes one or more nodes of the target document given by the
<i>xpath</i> attribute. Target Elements contain any number of action
elements.
action
The attributes <i>type</i> and <i>attribute</i> of action nodes specify
a action which sould be applied to the target element.
lib/XML/LibXML/Overlay.pm view on Meta::CPAN
# <isbn>9780596000271</isbn>
# </book>
# <book id="book2">
# <author>Elliotte Rusty Harold</author>
# <author>W. Scott Means</author>
# <title>XML in a Nutshell: A Desktop Quick Reference</title>
# <isbn>9780596007645</isbn>
# </book>
# </catalog>
# overlay.xml
####
# <overlay>
# <target xpath="/catalog/book[@id='book0']/author[text()='Delete Me!']">
# <action type="delete" />
# </target>
# <target xpath="/catalog/book[@id='book2']">
# <action type="insertBefore">
# <book id="book2">
# <author>Mark Jason Dominus</author>
# <title>Higher-Order Perl. Transforming Programs with Programs</title>
# <isbn>9781558607019</isbn>
# </book>
# </action>
# </target>
# </overlay>
use XML::LibXML;
use XML::LibXML::Overlay;
my $overlay = XML::LibXML::Overlay->load_xml(
'location' => '/path/to/overlay.xml',
);
my $target = XML::LibXML->load_xml(
'location' => '/path/to/target.xml',
);
$overlay->apply_to($target);
# do whatever you want with $target
=head1 DESCRIPTION
XML::LibXML::Overlay allowes to apply overlay files to XML files. This modul is
a rewirte of XML::Overlay, but it uses plain XML::LibXML instead of the Class::XML
thru XML::Parser stack.
=head1 DETAILS
XML::LibXML::Overlay inherits from XML::LibXML. So you can use XML::LibXML::Overlay
like XML::LibXML. The only difference is, that L</load_xml> returns a
XML::LibXML::Overlay::Document instead of a XML::LibXML::Document.
=head2 Tags
Following Tags can be used in a overlay document.
=head3 overlay
Specifies the root element, and contains any target element.
=head3 target
Selectes one or more nodes of the target document given by the <i>xpath</i> attribute.
Target Elements contain any number of action elements.
=head3 action
lib/XML/LibXML/Overlay/Document.pm view on Meta::CPAN
use warnings;
use base qw(XML::LibXML::Document);
use Carp qw( croak );
sub apply_to {
my $self = shift;
my ($target_doc) = @_;
my @overlay_targets = $self->findnodes('/overlay/target');
foreach my $overlay_target (@overlay_targets) {
my $xpath = $overlay_target->getAttribute('xpath');
if (not $xpath) {
croak "missing xpath attribute for target node";
}
my @actions = $overlay_target->findnodes('action');
# find all nodes target nodes in the target document
my @target_nodes = $target_doc->findnodes($xpath);
foreach my $target_node (@target_nodes) {
$self->_apply_actions($target_node, \@actions);
}
}
lib/XML/LibXML/Overlay/Document.pm view on Meta::CPAN
=head1 DETAILS
XML::LibXML::Overlay::Document inherits from XML::LibXML::Document. So you can
use XML::LibXML::Overlay::Document like XML::LibXML::Document.
=head1 METHODS
=head2 apply_to
$overlay->apply_to($target);
Takes a L<XML::LibXML::Document> and applies the changes specified by the $overlay
document. For more informations about who to use overlay see L<XML::LibXML::Overlay>.
=head1 SEE ALSO
L<XML::LibXML>, L<XML::Overlay>
=head1 AUTHOR
Alexander Keusch, C<< <kalex at cpan.org> >>
=head1 LICENSE
t/XML-LibXML-Overlay.t view on Meta::CPAN
use Test::More tests => 16;
use XML::LibXML;
BEGIN {
use_ok('XML::LibXML::Overlay');
use_ok('XML::LibXML::Overlay::Document');
};
# load overlay and target xml files
my $overlay = XML::LibXML::Overlay->load_xml(
'location' => 't/xml/overlay.xml',
);
ok( $overlay, 'created overlay document' );
ok( $overlay->isa('XML::LibXML::Overlay::Document'), 'document is a XML::LibXML::Overlay::Document' );
my $target = XML::LibXML->load_xml(
'location' => 't/xml/target.xml',
);
ok( $target, 'created target document' );
# applay the overlay to the target
$overlay->apply_to($target);
# appendChild
{
my @nodes = $target->findnodes("/catalog/book[\@id='book2']/author");
is ( scalar @nodes, 3, 'author node has been appended' );
is ( $nodes[2]->textContent(), 'Jon Orwant', 'appended node has the correct position' );
@nodes = $target->findnodes("//author[text()='Jon Orwant']");
is ( scalar @nodes, 1, 'node has been appended only once' );
}
t/xml/overlay.xml view on Meta::CPAN
<?xml version="1.0" encoding="UTF-8"?>
<overlay>
<target xpath="/catalog/book[@id='book2']">
<action type="appendChild">
<author>Jon Orwant</author>
</action>
</target>
<target xpath="/catalog/book[@id='book1']/author[text()='Delete Me!']">
<action type="delete" />
</target>
<target xpath="/catalog/book[@id='book1']">
<action type="insertBefore">
t/xml/overlay.xml view on Meta::CPAN
<isbn>9781884777790</isbn>
</book>
</action>
</target>
<target xpath="/catalog/book[@id='book5']">
<action type="setAttribute" attribute="myAttribute">attr</action>
</target>
<target xpath="/catalog/book">
<action type="removeAttribute" attribute="delete" />
</target>
</overlay>
( run in 0.336 second using v1.01-cache-2.11-cpan-65fba6d93b7 )