Some Utility Functions I Wrote Above XML::LibXML
Shlomi Fish <[email protected]>
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <[email protected]> |
Hi all!
Recently, I had to clean up an XHTML page from en.wikibooks.org . The page was
valid XHTML 1.0 transitional, but was quite convulted (as most MediaWiki pages
are) and so needed cleaning up so I can integrate it into my homepage. Since
it was valid XHTML, I decided to use XML::LibXML.
In the process of writing it I wrote a few utility functions, and I've been
wondering what to do with them. Possible options are:
1. Release as a separate module on CPAN.
2. Add to XML::LibXML as Perl-based methods.
3. Add to XML::LibXML as XS methods.
4. Add to libxml2 and then implement XML::LibXML bindings.
5. Do nothing with them and just keep them to myself.
You can find the full code here:
https://opensvn.csie.org/shlomif/homepage/trunk/bin/cleanup-mediawiki-html.pl
What I wrote reminds me of the beginning of a really lame jQuery/etc.
replacement only for XML. However http://search.cpan.org/dist/pQuery/ is
incomplete and HTML only, and does not support XML.
Anyway, here are the functions that I wrote:
<<<<<<<<<<<
sub remove
{
my $node = shift;
my $parent = $node->parentNode;
$parent->removeChild($node);
return;
}
>>>>>>>>>>>
Prunes a node from the XML tree, without knowing its parent in advance.
<<<<<<<<<<
sub empty
{
my $node = shift;
$node->removeChildNodes();
return $node;
}
>>>>>>>>>>
OK, this is pretty silly, but I thought it was not so trivial, so I ended up
writing it, and then re-implementing it proprely..
<<<<<<<<<<<<<<
sub stringify
{
my $list = shift;
return
[
map { (ref($_) eq "") ? XML::LibXML::Text->new($_) : $_ } @$list
];
}
sub replace
{
my $node = shift;
my $childs = shift;
empty($node);
foreach my $c (@{
stringify((ref($childs) eq "ARRAY") ? $childs : [$childs])
}
)
{
$node->appendChild($c);
}
return $node;
}
>>>>>>>>>>>>>>
Here, I replace a node with new child nodes.
Finally, there's also some pretty-complex code to remove all elements from a
document that come after a certain chosen element. It was not extracted into a
function yet (since I only used it once), but it would be possible.
Regards,
Shlomi Fish
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
What Makes Software Apps High Quality - http://xrl.us/bkeuk
God gave us two eyes and ten fingers so we will type five times as much as we
read.
_______________________________________________
Perl-XML mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs