示例#1
0
def test_abspath_with_ns():
    doc = parse(XML1)
    baz2 = doc.xml_first_child.xml_first_child
    ap = abspath(baz2)
    assert ap == u'/foo/baz'
    ap = abspath(baz2.xml_following_sibling)
    assert ap == u'/foo/bar:baz'
    ap = abspath(baz2.xml_following_sibling.xml_following_sibling)
    assert ap == u'/foo/baz[2]'
示例#2
0
def test_abspath_with_default_ns():
    doc = parse(XML2)
    baz = doc.xml_first_child.xml_first_child
    ap = abspath(baz)
    assert ap == u'/*[namespace-uri()="http://bax.com" and local-name()="foo"]/*[namespace-uri()="http://bax.com" and local-name()="baz"]'
    assert [baz] == list(doc.xml_select(ap))
    ap = abspath(baz.xml_following_sibling)
    assert ap == u'/*[namespace-uri()="http://bax.com" and local-name()="foo"]/bar:baz'
    assert [baz.xml_following_sibling] == list(doc.xml_select(ap))
    ap = abspath(baz, {u'bax': u'http://bax.com'})
    assert ap == u'/bax:foo/bax:baz'
    ap = abspath(baz.xml_following_sibling, {u'bax': u'http://bax.com'})
    assert ap == u'/bax:foo/bar:baz'
    dup1 = baz.xml_following_sibling.xml_following_sibling
    dup2 = dup1.xml_following_sibling
    ap = abspath(dup2, {u'bax': u'http://bax.com'})
    assert ap == u'/bax:foo/bax:dup[2]'
示例#3
0
 def report():
     common_ancestor_path = util.abspath(self.common_ancestor)
     print common_ancestor_path
     common_ancestor_path_len = len(common_ancestor_path)
     for wp in self.records[0]:
         wp.path = wp.path[self.common_ancestor_path_len+1:]
         print wp.path
         print wp.node
     return
示例#4
0
 def report():
     common_ancestor_path = util.abspath(self.common_ancestor)
     print common_ancestor_path
     common_ancestor_path_len = len(common_ancestor_path)
     for wp in self.records[0]:
         wp.path = wp.path[self.common_ancestor_path_len + 1:]
         print wp.path
         print wp.node
     return
示例#5
0
 def find(s):
     record = self.records[-1]
     expr = u'.//text()[contains(., "%s")]'%s.decode('utf-8')
     results = self.context.xml_select(expr)
     if not results:
         expr = u'.//@*[contains(., "%s")]'%s.decode('utf-8')
         results = self.context.xml_select(expr)
     wp = (results[0] if results else None)
     if wp:
         wp = waypoint(wp)
         wp.path = util.abspath(wp.node)
         if self.common_ancestor:
             while not self.common_ancestor in wp.node.xml_select('ancestor::*'):
                 self.common_ancestor = self.common_ancestor.xml_parent
         else:
             self.common_ancestor = wp.node.xml_parent
         record.append(wp)
     return
示例#6
0
文件: __init__.py 项目: mredar/amara
 def validate(self, node):
     '''
     Check this constraint against a node.
     Raise an exception if the constraint fails, possibly after attempting fixup
     '''
     from amara.bindery import BinderyError
     assertion = self.assertion
     if callable(assertion):
         assertion = assertion(node)
     result = datatypes.boolean(node.xml_select(assertion))
     if not result:
         if self.fixup:
             self.fixup(node)
             result = datatypes.string(node.xml_select(assertion))
             if result:
                 return
         raise BinderyError(BinderyError.CONSTRAINT_VIOLATION,
                            constraint=self.msg or assertion,
                            node=abspath(node))
示例#7
0
 def find(s):
     record = self.records[-1]
     expr = u'.//text()[contains(., "%s")]' % s.decode('utf-8')
     results = self.context.xml_select(expr)
     if not results:
         expr = u'.//@*[contains(., "%s")]' % s.decode('utf-8')
         results = self.context.xml_select(expr)
     wp = (results[0] if results else None)
     if wp:
         wp = waypoint(wp)
         wp.path = util.abspath(wp.node)
         if self.common_ancestor:
             while not self.common_ancestor in wp.node.xml_select(
                     'ancestor::*'):
                 self.common_ancestor = self.common_ancestor.xml_parent
         else:
             self.common_ancestor = wp.node.xml_parent
         record.append(wp)
     return
示例#8
0
 def validate(self, node):
     '''
     Check this constraint against a node.
     Raise an exception if the constraint fails, possibly after attempting fixup
     '''
     from amara.bindery import BinderyError
     assertion = self.assertion
     if callable(assertion):
         assertion = assertion(node)
     result = datatypes.boolean(node.xml_select(assertion))
     if not result:
         if self.fixup:
             self.fixup(node)
             result = datatypes.string(node.xml_select(assertion))
             if result:
                 return
         raise BinderyError(BinderyError.CONSTRAINT_VIOLATION, constraint=self.msg or assertion, node=abspath(node))