lxmlutil.etree module#

class lxmlutil.etree.ElementBase[source]#

Bases: ElementBase

This class extends the etree.ElementBase class. It has many additional userfriendly methods

deepcopy(*args, **kwargs)[source]#

Alias method for copy.deepcopy. See copy.deepcopy for more information

Returns:

duplicate of this element

dump(*args, **kwargs)[source]#

Alias method for etree.dump See etree.dump for more information.

findallqn(path, namespaces=None)[source]#

Similar to self.findall() with additional functionality of resolving path with prefixes

Parameters:
  • path – prefixed name paths, normal paths

  • namespaces – dictionary of prefix as key and namespace as value

Returns:

list of elements matching the given parameters

Examples:

e.findallqn("c:chart//c:areaChart/c:axId")
# [<Element {http://schemas.openxmlformats.org/drawingml/2006/chart}axId at 0x189d739f7a0>,
#  <Element {http://schemas.openxmlformats.org/drawingml/2006/chart}axId at 0x189d739f7f0>]

e.findallqn("./c:chart//c:areaChart/c:axId[@val="123456"]")
findqn(path, namespaces=None)[source]#

Similar to self.find() with additional funtionality of resolving path with prefixes.

Example:

# Following paths would be resolved to paths as shown below and passed to
# self.find() method and its return value is returned
'Relationship'      # -> 'Relationship'
'./Relationship'    # -> './Relationship'
'.//dummy'          # -> './/dummy'

# when self.nsmap = {'c':'http://cee/ns', 'r':'http://ree/ns'}
'.//c:autoUpdate'   # -> './/{http://cee/ns}autoUpdate'
'./c:chart//'       # -> './{http://cee/ns}chart//'
'.//c16:uniqueId'   # -> './/{http://c16/ns}uniqueId' when namespaces = {'c16':'http://c16/ns'}


'./c:chart//c:axId[@val="505253232"]'
    # -> './{http://cee/ns}chart//{http://cee/ns}axId[@val="505253232"]'
getqn(key, default=None)[source]#

Similar to self.get with additional flexibility. It can resolve the key with prefixes

Parameters:
  • key – element’s attribute name

  • default – this value would be returned when element do not have key attribute

Returns:

value or default of the key attribute of this element

Example:

# for element <c:somename val="0", r:id="rId3"/>
e.getqn("val") = "0"
e.getqn("r:id") = "rId3"
me(*args, **kwargs)[source]#

Alias for makeelement method See the definition of makelement method of etree.Elementbase for more details

meqn(tag, attrib=None, nsmap=None)[source]#

meqn is an abbreviation for makeelementqualifiedname

Parameters:
  • tag – It could be plane tag or tag with prefix eg. ‘Relationship’ or ‘c:chart’

  • attrib – It is dictionary with string key and string val. These will be attributes of the xml element

  • nsmap – If nsmap is None then nsmap of this element is used to resolve the prefix of the given tag

Returns:

created element object

Example:

# When self.nsmap == {}
self.meqn('c:chart')                            #-> Keyerror
self.meqn('foo')                                #-> element obj with tag 'foo'
self.meqn('a:foo', nsmap={'a':'http://Aaa/ns'}) #-> <{http://Aaa/ns}foo/>

self.meqn('a:foo', attrib={'val':'1'}, nsmap={'a':'http://Aaa/ns'})
    # -> <{http://Aaa/ns}foo "val"="1"/>

# when self.nsmap == {None: 'http://foo/ns', 'd':'htpp://deck/ns'}
self.meqn('some')                               #-> '{http://foo/ns}some'
self.meqn('some', nsmap={None:'http://bar'})    #-> '{http://bar}some'
self.meqn('c:some', nsmap={'c':'http://bar'})   #-> '{http://bar}some'
self.meqn('c:some')                             #-> KeyError: 'c'
self.meqn('c:some', nsmap={None:'http://bar'})  #-> KeyError: 'c'
self.meqn('d:some')                             #-> '{htpp://deck/ns}some'
qn(name, nsmap=None)[source]#

Handy method to get a qualified name from given arguments

Parameters:
  • name – prefixed or un-prefixed element name.

  • nsmap – dict with key as prefix and value as namespace

Returns:

fully qualified name (qn)

if nsmap is None, self.nsmap is used. if name has no prefix and nsmap do not have None returns name. if name has no prefix then nsmap must have None map.

Example:

# when self.nsmap -> {}
self.qn('some')                         #-> 'some'
self.qn('some', {None:'http://bar'})    #-> '{http://bar}some'
self.qn('c:some', {'c':'http://bar'})   #-> '{http://bar}some'
self.qn('c:some')                       #-> KeyError: 'c'
self.qn('c:some', {None:'http://bar'})  #-> KeyError: 'c'


# when self.nsmap -> {None: 'http://foo/ns', 'd':'htpp://deck/ns'}
self.qn('some')                         #-> '{http://foo/ns}some'
self.qn('some', {None:'http://bar'})    #-> '{http://bar}some'
self.qn('c:some', {'c':'http://bar'})   #-> '{http://bar}some'
self.qn('c:some')                       #-> KeyError: 'c'
self.qn('c:some', {None:'http://bar'})  #-> KeyError: 'c'
self.qn('d:some')                       #-> '{htpp://deck/ns}some'
rm()[source]#

Removes the current element from its parent. Limitation: it throws AttributeError when trying to remove root element

setqn(key, value)[source]#

Similar to self.set with additional flexibility. It can resolve the key with prefixes

Parameters:
  • key – element’s attribute name

  • value – key attribute of this element is set with the value

Example:

# for element <c:somename val="0", r:id="rId3"/>
e.setqn("val") = "1"
e.setqn("r:id") = "rId5"

# element would be now <c:somename val="1", r:id="rId5"/>
property ln#

Handy readonly property to get a local name of this element

Returns:

local name

Example:

# self.tag == '{http://some/ns}elemTag' -> self.ln == 'elemTag'
# self.tag == 'chart'                   -> self.ln == chart
property ns#

Readonly property.

Returns:

namespace of the element