lastTextNode = None
return tuple( result )
+def _processAttributes( attributes ) :
+
+ '''Clone attributes that are already part of another tree (which have
+ a parent).'''
+
+ result = []
+ for attribute in attributes :
+ if attribute.parent is not None :
+ attribute = attribute.clone()
+ result.append( attribute )
+ return tuple( result )
+
def _chainNodes( nodes , parent ) :
'''Update parent, prev and next reference for nodes in 'nodes'. The
raise NodeError( 'prev reference is already set' )
self.prev = node
+ def xpath( self , path ) :
+
+ import xpath
+ xpathObject = xpath.XPath( path )
+ return xpathObject.eval( self )
+
def __getitem__( self , path ) :
if not isinstance( path , basestring ) :
return super( Node , self ).__getitem__( path )
else :
- import xpath
- xpathObject = xpath.XPath( path )
- return xpathObject.eval( self )
+ return self.xpath( path )
# Abusing operator overloading
def __div__( self , path ) :
raise NotImplementedError
+ def match( self , pat ) :
+
+ import pattern
+ return pattern.test( self , pat )
+
def __cmp__( self , other ) :
if other is self :
self.name = name
self.children = children
- self.attributes = tuple( attributes )
+ self.attributes = _processAttributes( attributes )
_chainNodes( self.children , self )
_chainNodes( self.attributes , self )