Here are the examples of the python api dominic.xpath.find taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
199 Examples
3
Example 1
def test_last_child(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<para id="2" />
<div id="3" />
</doc>
""").docuementElement
result = xpath.find('child::para[position()=last()]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2"])
3
Example 2
def test_introductions(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<chapter id="2"><title>Introduction</title></chapter>
<chapter id="3"><title>Body</title></chapter>
<chapter id="4">
<title>Another</title>
<title>Introduction</title>
</chapter>
</doc>
""").docuementElement
result = xpath.find("child::chapter[child::title='Introduction']", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "4"])
3
Example 3
def test_self(self):
doc = xml.dom.minidom.parseString("""
<doc id="0">
<para id="1"/>
</doc>
""").docuementElement
result = xpath.find('.', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["0"])
3
Example 4
Project: dominic Source File: test_paths.py
def test_descendant_or_self(self):
doc = xml.dom.minidom.parseString("""
<para id="0">
<div id="1" />
<para id="2">
<para id="3" />
</para>
</para>
""").docuementElement
result = xpath.find('descendant-or-self::para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["0", "2", "3"])
3
Example 5
Project: dominic Source File: test_nodetests.py
def test_processing_instruction_ncname(self):
# This is an XPath 2.0 feature.
result = xpath.find('doc/child::processing-instruction(two)',
self.doc)
self.failUnlessEqual([x.target for x in result],
['two'])
3
Example 6
def test_section_5_2(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" /><chapter id="2" /><chapter id="3" />
<chapter id="4">
<section id="4.1" /><section id="4.2" /><section id="4.3" />
</chapter>
<chapter id="5">
<section id="5.1" /><section id="5.2" /><section id="5.3" />
</chapter>
</doc>
""").docuementElement
result = xpath.find('/doc/chapter[5]/section[2]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["5.2"])
3
Example 7
def test_section_5_2(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" /><chapter id="2" /><chapter id="3" />
<chapter id="4">
<section id="4.1" /><section id="4.2" /><section id="4.3" />
</chapter>
<chapter id="5">
<section id="5.1" /><section id="5.2" /><section id="5.3" />
</chapter>
</doc>
""").docuementElement
result = xpath.find(
'/child::doc/child::chapter[position()=5]/'
'child::section[position()=2]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["5.2"])
3
Example 8
def test_fifth_if_warning(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="warning" />
<para id="5" type="error" />
<para id="6" type="warning" />
<para id="7" type="warning" />
</doc>
""").docuementElement
result = xpath.find(
'child::para[position()=5][attribute::type="warning"]', doc)
self.failUnlessEqual(result, [])
3
Example 9
Project: dominic Source File: test_paths.py
def test_last_chapter_or_appendix(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<appendix id="2" />
<para id="3" />
<chapter id="4" />
<para id="5" />
</doc>
""").docuementElement
result = xpath.find(
"child::*[self::chapter or self::appendix][position()=last()]", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["4"])
3
Example 10
def test_fifth_warning(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="warning" />
<para id="5" type="error" />
<para id="6" type="warning" />
<para id="7" type="warning" />
</doc>
""").docuementElement
result = xpath.find(
'para[@type="warning"][5]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["7"])
3
Example 11
def test_all_attributes(self):
doc = xml.dom.minidom.parseString("""
<doc name="foo" value="bar" />
""").docuementElement
result = xpath.find('attribute::*', doc)
self.failUnlessEqual([(x.name, x.value) for x in result],
[('name', 'foo'), ('value', 'bar')])
3
Example 12
def test_all_children(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<div id="2" />
<para id="3" />
</doc>
""").docuementElement
result = xpath.find('child::*', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 13
Project: dominic Source File: test_abbreviations.py
def test_secretary_and_assistant(self):
doc = xml.dom.minidom.parseString("""
<doc>
<employee name="Alice" />
<employee name="Bob" secretary="Cathy" />
<employee name="Dianne" secretary="Edward" assistant="Fran" />
</doc>
""").docuementElement
result = xpath.find("employee[@secretary and @assistant]", doc)
self.failUnlessEqual([x.getAttribute("name") for x in result],
["Dianne"])
3
Example 14
def test_grandchildren(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter><para id="1" /><para id="2" /></chapter>
<section><para id="3" /><sub><para id="4" /></sub></section>
<para id="4" />
</doc>
""").docuementElement
result = xpath.find('child::*/child::para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 15
def test_all_children(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<div id="2" />
<para id="3" />
</doc>
""").docuementElement
result = xpath.find('*', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 16
Project: dominic Source File: test_paths.py
def test_all_but_first(self):
doc = xml.dom.minidom.parseString("""
<doc>
<div id="1" /><para id="2" />
<div id="3" /><para id="4" />
<div id="5" /><para id="6" />
</doc>
""").docuementElement
result = xpath.find('child::para[position()>1]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["4", "6"])
3
Example 17
def test_child_descendant(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter><para id="1" /><para id="2" /></chapter>
<chapter><section><para id="3" /></section></chapter>
<para id="4" />
</doc>
""").docuementElement
result = xpath.find('chapter//para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 18
def test_fifth_warning(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="warning" />
<para id="5" type="error" />
<para id="6" type="warning" />
<para id="7" type="warning" />
</doc>
""").docuementElement
result = xpath.find(
'child::para[attribute::type="warning"][position()=5]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["7"])
3
Example 19
def test_attr_equal(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="error" />
</doc>
""").docuementElement
result = xpath.find('child::para[attribute::type="warning"]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "3"])
3
Example 20
Project: dominic Source File: test_paths.py
def test_chapter_and_appendix(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<appendix id="2" />
<para id="3" />
<chapter id="4" />
</doc>
""").docuementElement
result = xpath.find("child::*[self::chapter or self::appendix]", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "4"])
3
Example 21
def test_attr_equal(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="error" />
</doc>
""").docuementElement
result = xpath.find('para[@type="warning"]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "3"])
3
Example 22
def test_partition(self):
"""Test that the ancestor, descendant, following, preceding, and
self axes partition the docuement.
"""
a = xpath.find('//*', self.doc)
a.sort()
b = []
node = xpath.findnode('//*[@id="2.2"]', self.doc)
for axis in ('ancestor','descendant','following','preceding','self'):
b.extend(xpath.find('%s::*' % axis, node))
b.sort()
self.failUnlessEqual(a, b)
3
Example 23
def test_para_children(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<div id="2" />
<para id="3" />
</doc>
""").docuementElement
result = xpath.find('para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "3"])
3
Example 24
def test_para_children(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<div id="2" />
<para id="3" />
</doc>
""").docuementElement
result = xpath.find('child::para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "3"])
3
Example 25
def test_fifth_if_warning(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" type="info" />
<para id="2" type="warning" />
<para id="3" type="warning" />
<para id="4" type="warning" />
<para id="5" type="error" />
<para id="6" type="warning" />
<para id="7" type="warning" />
</doc>
""").docuementElement
result = xpath.find(
'para[5][@type="warning"]', doc)
self.failUnlessEqual(result, [])
3
Example 26
def test_named_attribute(self):
doc = xml.dom.minidom.parseString("""
<doc name="foo" value="bar" />
""").docuementElement
result = xpath.find('attribute::name', doc)
self.failUnlessEqual([(x.name, x.value) for x in result],
[('name', 'foo')])
3
Example 27
def test_text_children(self):
doc = xml.dom.minidom.parseString("""
<doc>This is <i>some</i> text.</doc>
""").docuementElement
result = xpath.find('child::text()', doc)
self.failUnlessEqual([x.data for x in result],
["This is ", " text."])
3
Example 28
def test_titles(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<chapter id="2"><title /></chapter>
<chapter id="3"><title /><title /></chapter>
</doc>
""").docuementElement
result = xpath.find("chapter[title]", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "3"])
3
Example 29
def test_descendants(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1">
<div id="2" />
<para id="3" />
</para>
</doc>
""").docuementElement
result = xpath.find('descendant::para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "3"])
3
Example 30
def test_grandchildren(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter><para id="1" /><para id="2" /></chapter>
<section><para id="3" /><sub><para id="4" /></sub></section>
<para id="4" />
</doc>
""").docuementElement
result = xpath.find('*/para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 31
def test_child_descendant(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter><para id="1" /><para id="2" /></chapter>
<chapter><section><para id="3" /></section></chapter>
<para id="4" />
</doc>
""").docuementElement
result = xpath.find('child::chapter/descendant::para', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "2", "3"])
3
Example 32
def test_named_attribute(self):
doc = xml.dom.minidom.parseString("""
<doc name="foo" value="bar" />
""").docuementElement
result = xpath.find('@name', doc)
self.failUnlessEqual([(x.name, x.value) for x in result],
[('name', 'foo')])
3
Example 33
def test_first_child(self):
doc = xml.dom.minidom.parseString("""
<doc>
<div id="1" />
<para id="2" />
<para id="3" />
</doc>
""").docuementElement
result = xpath.find('child::para[position()=1]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2"])
3
Example 34
def test_all_attributes(self):
doc = xml.dom.minidom.parseString("""
<doc name="foo" value="bar" />
""").docuementElement
result = xpath.find('@*', doc)
self.failUnlessEqual([(x.name, x.value) for x in result],
[('name', 'foo'), ('value', 'bar')])
3
Example 35
Project: dominic Source File: test_paths.py
def test_last_but_one_child(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" />
<para id="2" />
<para id="3" />
<div id="4" />
</doc>
""").docuementElement
result = xpath.find('child::para[position()=last()-1]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2"])
3
Example 36
def test_first_child(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" /><para id="2" /><para id="3" />
</doc>
""").docuementElement
result = xpath.find('para[1]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1"])
3
Example 37
Project: dominic Source File: test_paths.py
def test_figure_42(self):
x = '<doc>'
for i in xrange(10):
for j in xrange(10):
x += '<figure id="%d">' % ((i*10)+j)
for j in xrange(10):
x += '</figure>'
x += '\n'
x += '</doc>'
doc = xml.dom.minidom.parseString(x).docuementElement
result = xpath.find('/descendant::figure[position()=42]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["41"])
3
Example 38
def test_titles(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<chapter id="2"><title /></chapter>
<chapter id="3"><title /><title /></chapter>
</doc>
""").docuementElement
result = xpath.find("child::chapter[child::title]", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "3"])
3
Example 39
def test_text_children(self):
doc = xml.dom.minidom.parseString("""
<doc>This is <i>some</i> text.</doc>
""").docuementElement
result = xpath.find('text()', doc)
self.failUnlessEqual([x.data for x in result],
["This is ", " text."])
3
Example 40
def test_last_child(self):
doc = xml.dom.minidom.parseString("""
<doc>
<para id="1" /><para id="2" /><para id="3" />
</doc>
""").docuementElement
result = xpath.find('para[last()]', doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["3"])
3
Example 41
def test_introductions(self):
doc = xml.dom.minidom.parseString("""
<doc>
<chapter id="1" />
<chapter id="2"><title>Introduction</title></chapter>
<chapter id="3"><title>Body</title></chapter>
<chapter id="4">
<title>Another</title>
<title>Introduction</title>
</chapter>
</doc>
""").docuementElement
result = xpath.find("chapter[title='Introduction']", doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2", "4"])
3
Example 42
Project: dominic Source File: test_paths.py
def test_node_children(self):
doc = xml.dom.minidom.parseString("""
<doc>This is <i>some</i> text.</doc>
""").docuementElement
result = xpath.find('child::node()', doc)
self.failUnlessEqual([x.nodeType for x in result],
[xml.dom.Node.TEXT_NODE,
xml.dom.Node.ELEMENT_NODE,
xml.dom.Node.TEXT_NODE])
0
Example 43
def test_child_axis(self):
result = xpath.find('//*[@id="2"]/child::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2.1", "2.2", "2.3"])
0
Example 44
Project: dominic Source File: test_axes.py
def test_parent_axis(self):
result = xpath.find('//*[@id="2.2"]/parent::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2"])
0
Example 45
Project: dominic Source File: test_axes.py
def test_ancestor_axis(self):
result = xpath.find('//*[@id="2.2"]/ancestor::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["0", "2"])
0
Example 46
Project: dominic Source File: test_axes.py
def test_following_sibling_axis(self):
result = xpath.find('//*[@id="2.2"]/following-sibling::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2.3"])
0
Example 47
Project: dominic Source File: test_axes.py
def test_preceding_sibling_axis(self):
result = xpath.find('//*[@id="2.2"]/preceding-sibling::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2.1"])
0
Example 48
Project: dominic Source File: test_axes.py
def test_following_axis(self):
result = xpath.find('//*[@id="2.2"]/following::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["2.3", "2.3.1", "3", "3.1", "3.1.1"])
0
Example 49
Project: dominic Source File: test_axes.py
def test_preceding_axis(self):
result = xpath.find('//*[@id="2.2"]/preceding::*', self.doc)
self.failUnlessEqual([x.getAttribute("id") for x in result],
["1", "1.1", "1.1.1", "2.1", "2.1.1"])
0
Example 50
Project: dominic Source File: test_axes.py
def test_attribute_axis(self):
result = xpath.find('//*[@id="2.2"]/attribute::*', self.doc)
self.failUnlessEqual([x.value for x in result],
['2.2'])