=head1	NAME

ODF::lpOD::Element - Common features available with any ODF element


This manual page describes the C<odf_element> class.

C<odf_element> is an alias for C<ODF::lpOD::Element> package.

Every XML element (loaded from an existing ODF document or created by any
lpOD-based application) is a C<odf_element>. This class is the base class
for any document element; its features are inherited by other, more specialized
element classes.

An element may be explicitly created using the C<odf_create_element> class
constructor (or a the constructor of any derivative of C<odf_element> (such
as C<odf_paragraph>, C<odf_table>, etc), then inserted somewhere is an document.
However, in most cases, elements are either retrieved in the existing structure
or implicitly created ad put in place as children of existing elements through
various element-based C<set_xxx> methods (where "xxx" depends on the kind of
newly created data).

Among the C<odf_element> methods, we distinguish I<element> methods from
I<context> methods, while both kinds of methods belong to C<odf_element>
objects. An element method gets or sets one or more properties of the calling
element, while a context method uses the calling element as its operating
context and may produce effects regarding other elements located somewhere in
the hierarchy of the calling element (generally below, and sometimes above).
As examples, C<set_attribute> is an element method (it changes an attribute of
the current element), while C<get_element> (in its element-based version, that
is not the same as its part-based one) retrieves an element somewhere below
the current one.

=head1  Constructor and retrieval tools

=head3  odf_create_element(data)

Creates an odf_element from a fragment of XML data or an arbitrary tag.
If the given argument is valid XML, it's parsed and the new element is
created accordingly, possibly with a complex structure. If the argument
is a non-XML string, it's regarded as a tag (possibly with a name space
prefix), and the new element is created internally without XML parsing. 

The new element is not attached to a document; it's free for later use.

Note: C<odf_create_element()> is an alias for one of the following
instructions, which are equivalent:


=head3  get_element(tag [options])

This method returns the first element (if any) matching the given XML tag.
It's the most generic context-based retrieval method.

The given tag may be replaced by a regular expression, so the search space will
include all the elements whose tags match the expression.

For example, the following instruction (assuming C<$context> is a previously
retrieved element) returns the first element that is either a paragraph or a
heading (knowing that the corresponding tags are C<text:p> and C<text:h>):

        my $text_element = $context->get_element(qr'text:(p|h)'); 

The allowed options are:



C<position>: The sequential zero-based position of the element among the
set of elements matching the given tag; negative positions are counted
backward from the end.


C<attribute>: The name of an attribute used as a selection criterium; if this
option is set, the C<value> option is required.


C<value>: the value of the selection attribute.


C<content>: a search string (or a regexp) restricting the search space to the
elements with matching content.


The example below (that combines all the options) returns the 4th level 1
heading before the end of the current context:

                attribute       => 'outline level',
                value           => 1,
                position        => -4

I<Caution: the C<get_element> method of C<odf_part> is not the same as the
C<get_element> method of C<odf_element>.>

=head3  get_elements(tag)

Returns the full list of elements matching the given tag, whose tags match
the given regexp.

The C<attribute> and C<value> options are allowed in order to restrict the

The next example returns the list of paragraphs whose style is "Standard":

        my @std_paragraphs = $context->get_elements(
                attribute       => 'style name',
                value           => 'Standard'

=head3  get_parent

This method returns the immediate I<parent> of the calling element. Of course,
it returns C<undef> if the context element is itself a C<root>, or if it's
not included yet in a document.

=head3  get_root

Returns the top level element of the document part that contains the calling

=head3  get_document

Returns the C<odf_document> instance to which the element belongs.
Returns C<undef> if the element is not attached to a C<odf_document>.

=head1  Top level contexts

As introduced in L<ODF::lpOD::Document>, the C<odf_part> handlers provide
methods that automatically return high level elements that may be the preferred
contexts in most cases. The most common one is the I<root> element; its context
is the whole document part. The C<body> element, that is sometimes the same as
the C<root> one, is a bit more restricted in the document C<content> part (it
includes only the content objects, and excludes other objects such as style
definitions). Both the C<root> and the C<body> may be got using the part-based
C<get_root> and C<get_body> methods.

The following sequence, starting from the creation of a document instance,
selects a part, then the root element of the part, than selects the list of
table styles defined in the part:

        my $doc = odf_get_container("/home/jmg/report.odt");
        my $content = $doc->get_content;
        my $context = $content->get_root;
        my @table_styles = $context->get_element_list(
                attribute       => 'family',
                value           => 'table'

Note that in this last example nothing could be found knowing that style
elements are not allowed by the ODF specification in the C<body> context.

=head1  Child element creation methods

The methods described in this section allows the user to insert elements
(previously existing or not) as children of the calling element.

=head3  insert_element(element [options])

Insert the given odf_element at a given position, that is defined according
to a C<position> parameter, whose possible values are:



C<FIRST_CHILD>: the odf_element will be the first child (default).


C<LAST_CHILD>: the odf_element will be the last child.


C<NEXT_SIBLING>: the odf_element will be inserted just after.


C<PREV_SIBLING>: the odf_element will be inserted just before.


C<WITHIN>: the odf_element will be inserted as a child within the text content;
if C<position> is C<WITHIN>, then the C<offset> parameter is required.


C<offset>: specifies the position in the text of the context element where the
new child element must be inserted (the position is zero-based).


C<before>: the value of this option, if set, must be another child
C<odf_element> of the calling one; the new element will be inserted as the
previous sibling of this child element.


C<after>: like C<before>, but the new element will be inserted I<after> the
value of this option.


The WITHIN option splits the text content of the container in two parts
and inserts the elements between them, at a given offset. So if position is
WITHIN, the offset optional parameter is used.
By default, if no offset argument is provided, or if the calling element
doesn't contain any text, WITHIN produces the same result as FIRST_CHILD.
The offset argument must be an integer; it specifies the position of the
inserted child element within the text content of the calling element.
A zero offset means that the element must be inserted before the 1st
character. A negative offset value means that the insert position must be
counted down from the end of the text, knowing that -1 is the position just
before the last character. Of course, if the insertion must be done after
the end of the text, the simplest way is to select LAST_CHILD instead of

If C<before> or C<after> is provided, the other options are ignored. Of course,
C<before> and C<after> are mutually exclusive.

The following example inserts a previously existing element between the 4th and
the 5th characters of the text of the calling element:

                position        => WITHIN,
                offset          => 4

The next example inserts a new empty paragraph before the last paragraph of the
calling context:

        my $last_p = $context->get_element('text:p', position => -1);
                before          => $last_p

(Note that smarter methods, described elsewhere, may produce the same results).

=head3  insert_element(tag)

Like the first version of C<insert_element>, but the argument is an XML tag
(i.e. technically a text string instead of a C<odf_element> instance); in
such a case a new element is created then inserted according to the same rules
and options.

=head3  append_element(element/tag)

Like C<insert_element>, but without options; appends the element as the I<last
child> of the calling element. So these tow lines are equivalent:

        $context->insert_element($elt, position => LAST_CHILD);

=head1  Element methods

The methods introduced in this section are accessors that get or set the
own properties of the calling element. However, in some cases they may have
indirect consequence on other elements.

=head3  clear

Erases the text of an element and all its children. Beware that this method
is overiden by some specialized element classes.

=head3  clone

Returns a copy of the calling element, with all its attributes, its text, and
its children elements. Allows the user to copy a high-level structured element
(like a section or a table) as well as a single paragraph. The copy is a free
element, that may be inserted somewhere in the same document as the prototype,
or in another document.

=head3  delete

Removes the calling element with all its descendants.

=head3  del_attribute(name)

Deletes the attribute whose name is given in argument. Nothing is done if
the attribute doesn't exist. The argument may be the exact XML name of the
attribute, or an "approximative" name according to the same logic as
C<get_attribute> below.

=head3  get_attribute(name)

Returns the string value of the attribute having this name. The argument may
be the exact XML name of the attribute. However, if a name without name space
prefix is provided, the prefix is automatically supposed to be the same as the
prefix of the context element. In addition, any white space or underscore
character in the given name is interpreted as a "-". As a consequence, some
attributes may be designated without care of the exact XML syntax. As an
example, assuming C<$p> is a paragraph, the two instructions below are
equivalent, knowing that the name space prefix of a paragraph is C<'text'>:

        $style = $p->get_attribute('text:style-name');
        $style = $p->get_attribute('style name');

The attribute values are returned in a character set that depends on the
global configuration. See L<ODF::lpOD::Common> for details about the character
set handling.

=head3  get_attributes

Returns all the attributes of the calling element as a hash ref where keys are
the full XML names of the attributes.

=head3  get_style

Returns the name of the style used by the calling element (this accessor makes
sense for objects that may be displayed according to a layout). Returns
C<undef> if no style is used.

Note: if your style names contain non-ASCII characters and if your preferred
output character set is not C<utf8>, see L<ODF::lpOD::Common> for details
about character sets handling.

=head3  get_tag

Returns the XML tag of the element with its name space prefix.

=head3  get_text(recursive => FALSE)

Returns the text contents of the element as a string. By default this method is
not recursive (i.e. it just returns the own text of the element, not the text
belonging to children and descendant elements). However, if the optional
C<recursive> parameter is provided and set to C<TRUE>, then the method returns
the concatenated contents of all the descendants of the given element.

In a default configuration, the character set of the output is C<utf8>. If
that is not convenient for you, see the character set handling section in

=head3  get_url

Returns the URL if the element owns a hyperlink property, or C<undef> otherwise.

=head3  serialize

Returns an XML export of the calling element, allowing the lpOD applications to
store and/or transmit particular pieces of documents, and not only full
documents. The C<pretty> option is allowed, like with the C<serialize> method
of C<odf_part> objects, described in L<ODF::lpOD::Document>.

Note that this XML export is not affected by the content encoding/decoding
mechanism that works for user content, so it's character doesn't depend on
the custom text output character set possibly selected through the
C<set_output_charset()> method introduced in L<ODF::lpOD::Common>.

=head3  set_attribute(attribute => value)

Sets or changes an attribute of the calling element. The attribute is created
if it didn't exist. If the provided value is C<undef>, the attribute is
deleted (if it didn't exist, nothing is done). The attribute name may be
specified according to the same rules as with C<get_attribute>.

About the character set of the input values, the same rules as with any text
input apply; see the character set handling section in L<ODF::lpOD::Common>.

=head3  set_attributes(attr_hash_ref)

Sets several attributes at a time. The attributes to change or create must be
passed as a hash ref (like the hash ref returned by C<get_attributes>). The
attribute names may be provided in simplified form like with C<set_attribute>.

=head3  set_comment(text)

Intended for debugging purposes, this method puts a XML comment I<before> the
calling element. This comment produces a "<!--xyz-->" tag, where "xyz" is the
given text, in the XML output if the document is later serialized. Beware that
such comments are not always preserved if the document is changed by an office
application software.

=head3  set_child(tag, text, attributes)

Synonym of C<set_first_child()> (see below).

=head3  set_first_child(tag, text, attributes)

Makes sure that the calling element contains at least one element with the
given XML tag. If there is no compliant child, a new element is created with
the given tag and inserted as the first child of the calling element. If one
or more compliant child exist, the first one is selected and its text content
(if any) is deleted.

The second argument (optional) is a string that becomes the new text content
of the created or selected child. The remainder of the argument list, if any,
is a hash specifying attribute/value pairs for this element.

The return value is the selected or created element.

=head3  set_last_child(tag, text, attributes)

Same as C<set_first_child()> but, in case of creation, the new element is
inserted as the I<last> child. If compliant children already exist, the
result is the same as C<set_first_child()>.

=head3  set_parent(tag, text, attributes)

Makes sure that the current element is a child of an element whose tag is
specified by the first argument. If the calling element is free or if its
immediate parent has not the given tag, a new element with the given tag is
inserted as the same place of the calling element, that becomes the first
child of the new element.

The return value is the new element.

The other arguments are the same as with C<set_first_child()>.

=head3  set_style(style_name)

Changes or sets the style name of the calling object. Caution: a lot of ODF
elements should not have any style, so don't use this accessor unless you know
that the calling object needs a style.

Note: if your style names contain non-ASCII characters and if your preferred
input character set is not C<utf8>, see L<ODF::lpOD::Common> for details
about character sets handling.

=head3  set_tag(new_tag)

Changes the XML tag of the calling element. Not for usual business; it's a low
level technical feature.

=head3  set_text(text_string)

Sets the text content of the calling element, replacing any previous content.

        my $paragraph = $context->get_element('text:p', position => 15);
        $paragraph->set_text("The new content");

The character set of the provided string must comply to the currently active
input character set (default is C<utf8>). See the character set handling
section in L<ODF::lpOD::Common> if you get troubles about text encoding.

If C<set_text> is called with an empty string, its effect is the same as

=head3  set_url(url)

Sets a URL attribute (C<xlink:href>) with the argument.

=head1  Custom element classes

The C<ODF::lpOD::Element> package provides a C<associate_tag> class method
allowing developers to create custom subclasses and associate them to
particular elements. The code example below defines C<CustomParagraph> as a
subclass of C<odf_pragraph> (introduced in L<ODF::lpOD::TextElement>) and
specifies that every C<text:p> XML element must be mapped to this new
class instead of C<ODF::lpOD::Paragraph>:

        package CustomParagraph;
        use ODF::lpOD;
        use base 'ODF::lpOD::Paragraph';
        sub custom_method {

This extensibility mechanism must be used very cautiously if the specified tag
is already associated with a lpOD class, knowing that a wrongly overriden
method could produce destructive side effects.


Copyright (c) 2010 Ars Aperta, Itaapy, Pierlis, Talend.

This work was sponsored by the Agence Nationale de la Recherche

lpOD is free software; you can redistribute it and/or modify it under
the terms of either:

a) the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version.
lpOD is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with lpOD.  If not, see L<http://www.gnu.org/licenses/>.

b) the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at