XML::Element - XML elements with the same interface as HTML::Element


XML-TreeBuilder documentation Contained in the XML-TreeBuilder distribution.

Index


Code Index:

NAME

Top

XML::Element - XML elements with the same interface as HTML::Element

SYNOPSIS

Top

  [See HTML::Element]

METHODS AND ATTRIBUTES

Top

delete_ignorable_whitespace

TODO: test and document this: with no tagname set, assumes ALL all-whitespace nodes are ignorable!

endtag

Redirects to HTML::Element::endtag_XML

starttag

Redirects to HTML::Element::starttag_XML

DESCRIPTION

Top

This is just a subclass of HTML::Element. It works basically the same as HTML::Element, except that tagnames and attribute names aren't forced to lowercase, as they are in HTML::Element.

HTML::Element describes everything you can do with this class.

CAVEATS

Top

Has currently no handling of namespaces.

SEE ALSO

Top

XML::TreeBuilder for a class that actually builds XML::Element structures.

HTML::Element for all documentation.

XML::DOM and XML::Twig for other XML document tree interfaces.

XML::Generator for more fun.

COPYRIGHT AND DISCLAIMERS

Top

AUTHOR

Top

Current Author: Jeff Fearn <jfearn@cpan.org>.

Former Authors: Sean M. Burke, <sburke@cpan.org>


XML-TreeBuilder documentation Contained in the XML-TreeBuilder distribution.

require 5;

package XML::Element;
use warnings;
use strict;
use HTML::Tagset ();
use HTML::Element 4.1 ();

use vars qw(@ISA $VERSION);
$VERSION = 4.1;
@ISA     = ('HTML::Element');

# Init:
my %emptyElement = ();
foreach my $e (%HTML::Tagset::emptyElement) {
    $emptyElement{$e} = 1
        if substr( $e, 0, 1 ) eq '~' and $HTML::Tagset::emptyElement{$e};
}

#--------------------------------------------------------------------------
#Some basic overrides:

sub _empty_element_map { \%emptyElement }

*_fold_case      = \&HTML::Element::_fold_case_NOT;
*starttag        = \&HTML::Element::starttag_XML;
*endtag          = \&HTML::Element::endtag_XML;
*encoded_content = \$HTML::Element::encoded_content;

# TODO: override id with something that looks for xml:id too/instead?

#--------------------------------------------------------------------------

#TODO: test and document this:
# with no tagname set, assumes ALL all-whitespace nodes are ignorable!

sub delete_ignorable_whitespace {
    my $under_hash = $_[1];
    my (@to_do) = ( $_[0] );

    if ( $under_hash and ref($under_hash) eq 'ARRAY' ) {
        $under_hash = { map { ; $_ => 1 } @$under_hash };
    }

    my $all = !$under_hash;
    my ( $i, $this, $children );
    while (@to_do) {
        $this = shift @to_do;
        $children = $this->content || next;
        if ( ( $all or $under_hash->{ $this->tag } )
            and @$children )
        {
            for ( $i = $#$children; $i >= 0; --$i ) {

                # work backwards thru the list
                next if ref $children->[$i];
                if ( $children->[$i] =~ m<^\s*$>s ) {    # all WS
                    splice @$children, $i, 1;            # delete it.
                }
            }
        }
        unshift @to_do, grep ref($_), @$children;        # recurse
    }

    return;
}

#--------------------------------------------------------------------------

1;

__END__