Changeset 2804:65d88f7dbd37 in livinglogic.python.xist

Show
Ignore:
Timestamp:
07/23/07 15:40:28 (12 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add namespace name check ability to xfind.hasname and xfind.hasnameattr_xml.

xfind.hasname and xfind.hasnameattr_xml now have a second argument. If it is
specified it a a namespace name (or module) and the selector checks that the
node is an element from this namespace.

Files:
2 modified

Legend:

Unmodified
Added
Removed
  • src/ll/xist/xfind.py

    r2802 r2804  
    174174    """ 
    175175    <par>Selector that selects all nodes that have a specified Python name (which 
    176     only selects elements, processing instructions and entities).</par> 
     176    only selects elements, processing instructions and entities). Also a namespace 
     177    name can be specified as a second argument, which will only select elements 
     178    from the specified namespace.</par> 
    177179 
    178180    <example> 
     
    190192    </example> 
    191193    """ 
    192     def __init__(self, name): 
     194    def __init__(self, name, xmlns=None): 
    193195        self.name = name 
    194  
    195     def match(self, path): 
    196         if path: 
    197             node = path[-1] 
    198             return IsInstanceSelector(node, (xsc.Element, xsc.ProcInst, xsc.Entity)) and node.__class__.__name__ == self.name 
     196        self.xmlns = xsc.nsname(xmlns) 
     197 
     198    def match(self, path): 
     199        if path: 
     200            node = path[-1] 
     201            if self.xmlns is not None: 
     202                return isinstance(node, xsc.Element) and node.__class__.__name__ == self.name and node.xmlns == self.xmlns 
     203            else: 
     204                return isinstance(node, (xsc.Element, xsc.ProcInst, xsc.Entity)) and node.__class__.__name__ == self.name 
    199205        return False 
    200206 
     
    208214    except that the specified name is treated as the &xml; name, not the Python name. 
    209215    """ 
    210     def __init__(self, name): 
     216    def __init__(self, name, xmlns=None): 
    211217        self.name = name 
    212  
    213     def match(self, path): 
    214         if path: 
    215             node = path[-1] 
    216             return IsInstanceSelector(node, (xsc.Element, xsc.ProcInst, xsc.Entity)) and node.xmlname == self.name 
     218        self.xmlns = xsc.nsname(xmlns) 
     219 
     220    def match(self, path): 
     221        if path: 
     222            node = path[-1] 
     223            if self.xmlns is not None: 
     224                return isinstance(node, xsc.Element) and node.xmlname == self.name and node.xmlns == self.xmlns 
     225            else: 
     226                return isinstance(node, (xsc.Element, xsc.ProcInst, xsc.Entity)) and node.xmlname == self.name 
    217227        return False 
    218228 
     
    323333class onlyoftype(Selector): 
    324334    """ 
    325     <par>Selector that selects all nodes that the only nodes of their type among 
     335    <par>Selector that selects all nodes that are the only nodes of their type among 
    326336    their siblings.</par> 
    327337 
  • test/test_xfind.py

    r2788 r2804  
    9191    def check(expr, res): 
    9292        assert [str(e) for e in node.walknode(expr)] == res 
    93     yield check, xfind.hasname("em"), ["important", "first", "second", "important", "first", "important", "second", "important", "only"] 
    94     yield check, xfind.hasname_xml("em"), ["important", "first", "second", "important", "first", "important", "second", "important", "only"] 
     93    result = ["important", "first", "second", "important", "first", "important", "second", "important", "only"] 
     94    yield check, xfind.hasname("em"), result 
     95    yield check, xfind.hasname("em", html), result 
     96    yield check, xfind.hasname("em", html.xmlns), result 
     97    yield check, xfind.hasname("em", "gurk"), [] 
     98    yield check, xfind.hasname_xml("em"), result 
     99    yield check, xfind.hasname_xml("em", html), result 
     100    yield check, xfind.hasname_xml("em", html.xmlns), result 
     101    yield check, xfind.hasname_xml("em", "gurk"), [] 
    95102 
    96103