/usr/share/perl5/XML/Normalize/LibXML.pm is in libxml-xupdate-libxml-perl 0.6.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | # $Id: LibXML.pm,v 1.8 2003/12/03 12:01:14 pajas Exp $
package XML::Normalize::LibXML;
use strict;
use Exporter;
use XML::LibXML;
use XML::LibXML::Iterator;
use vars qw(@ISA @EXPORT_OK $VERSION);
BEGIN {
@ISA = qw(Exporter);
@EXPORT_OK = qw(trim xml_normalize xml_strip_whitespace xml_strip_element);
$VERSION='0.2'
}
sub trim {
my ($text)=@_;
$text=~s/^\s*//;
$text=~s/\s*$//;
return $text;
}
sub xml_normalize {
my ($node) = @_;
my $prev = undef; # previous Text node
foreach my $node ($node->childNodes()) {
my $type = $node->nodeType;
if ($type == XML::LibXML::XML_TEXT_NODE) {
if ($prev) {
$prev->setData($prev->getData().$node->getData());
$node->unbindNode();
} else {
$prev=$node;
}
} else {
$prev = undef;
if ($type == XML::LibXML::XML_ELEMENT_NODE) {
xml_normalize($node);
}
}
}
}
sub xml_strip_whitespace_text_node {
my ($node)=@_;
if ($node->nodeType() == XML::LibXML::XML_TEXT_NODE) {
my $data=trim($node->getData());
if ($data ne "") {
$node->setData($data);
} else {
$node->unbindNode();
}
}
}
sub xml_strip_whitespace_attributes {
my ($node)=@_;
if ($node->nodeType() == XML::LibXML::XML_ELEMENT_NODE) {
foreach my $attr ($node->attributes()) {
$node->setValue(trim($node->getValue()));
}
}
}
sub xml_strip_whitespace {
my ($dom, $strip_attributes)=@_;
xml_normalize($dom);
my $iter= XML::LibXML::Iterator->new( $dom );
if ($strip_attributes) {
$iter->iterate(sub {
xml_strip_whitespace_attributes($_[1]);
xml_strip_whitespace_text_node($_[1]);
});
} else {
$iter->iterate(sub {
xml_strip_whitespace_text_node($_[1]);
});
}
}
sub xml_strip_element {
my ($node)=@_;
return unless $node->nodeType == XML::LibXML::XML_ELEMENT_NODE;
my $f = $node->firstChild;
while ($f and $f->nodeType == XML::LibXML::XML_TEXT_NODE and
$f->getData =~ /^\s/) {
my $data=$f->getData();
$data=~s/^\s+//;
if ($data ne "") {
$f->setData($data);
last;
} else {
$f->unbindNode();
$f = $node->firstChild;
}
}
my $f = $node->lastChild;
while ($f and $f->nodeType == XML::LibXML::XML_TEXT_NODE and
$f->getData =~ /\s$/) {
my $data=$f->getData();
$data=~s/\s+$//;
if ($data ne "") {
$f->setData($data);
last;
} else {
$f->unbindNode();
$f = $node->lastChild;
}
}
}
1;
__END__
=pod
=head1 NAME
XML::Normalize::LibXML - simple whitespace striping functions
=head1 SYNOPSIS
use XML::Normalize::LibXML qw(trim xml_normalize xml_strip_whitespace);
$greeting=trim(" hallo world "); # returns "hallo world"
xml_normalize($dom->getDocumentElement());
xml_strip_whitespace($dom->getDocumentElement());
=head1 DESCRIPTION
This module provides simple whitespace striping and text-node
normalizing functions.
=head2 C<trim($string)>
Returns the string with any whitespace occuring at its beginning or
end removed.
=head2 C<xml_normalize($dom)>
Puts all Text nodes in the full depth of the sub-tree underneath this
Element into a normal form where only markup (e.g., tags,
comments, processing instructions, CDATA sections, and entity
references) separates Text nodes, i.e., there are no adjacent Text
nodes. This can be used to ensure that the DOM view of a document is
the same as if it were saved and re-loaded, and is useful when
operations (such as XPointer lookups) that depend on a particular
document tree structure are to be used.
=head2 C<xml_strip_whitespace($dom [,$include_attributes])>
Normalizes the subtree and trims whitespace from all Text nodes within
the subtree. If the optional argument $include_attributes is defined
and non-zero, this function trims whitespace also from all Attribute
nodes.
=head2 C<xml_strip_element($node)>
Removes leading and trailing whitespace from a given element.
=head1 AUTHOR
Petr Pajas, pajas@matfyz.cz
=head1 COPYRIGHT
Copyright 2002-2003 Petr Pajas, All rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=head1 SEE ALSO
L<XML::LibXML>
=cut
|