本文整理汇总了Python中twisted.web.microdom.parseString函数的典型用法代码示例。如果您正苦于以下问题:Python parseString函数的具体用法?Python parseString怎么用?Python parseString使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了parseString函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: footnotes
def footnotes(document):
"""
Find footnotes in the given document, move them to the end of the body, and
generate links to them.
A footnote is any node with a C{class} attribute set to C{footnote}.
Footnote links are generated as superscript. Footnotes are collected in a
C{ol} node at the end of the document.
@type document: A DOM Node or Document
@param document: The input document which contains all of the content to be
presented.
@return: C{None}
"""
footnotes = domhelpers.findElementsWithAttribute(document, "class", "footnote")
if not footnotes:
return
footnoteElement = microdom.Element("ol")
id = 1
for footnote in footnotes:
href = microdom.parseString('<a href="#footnote-%(id)d">' "<super>%(id)d</super></a>" % vars()).documentElement
text = " ".join(domhelpers.getNodeText(footnote).split())
href.setAttribute("title", text)
target = microdom.Element("a", attributes={"name": "footnote-%d" % id})
target.childNodes = [footnote]
footnoteContent = microdom.Element("li")
footnoteContent.childNodes = [target]
footnoteElement.childNodes.append(footnoteContent)
footnote.parentNode.replaceChild(href, footnote)
id += 1
body = domhelpers.findNodesNamed(document, "body")[0]
header = microdom.parseString("<h2>Footnotes</h2>").documentElement
body.childNodes.append(header)
body.childNodes.append(footnoteElement)
示例2: testNamespaces
def testNamespaces(self):
s = '''
<x xmlns="base">
<y />
<y q="1" x:q="2" y:q="3" />
<y:y xml:space="1">here is some space </y:y>
<y:y />
<x:y />
</x>
'''
d = microdom.parseString(s)
# at least make sure it doesn't traceback
s2 = d.toprettyxml()
self.assertEquals(d.documentElement.namespace,
"base")
self.assertEquals(d.documentElement.getElementsByTagName("y")[0].namespace,
"base")
self.assertEquals(
d.documentElement.getElementsByTagName("y")[1].getAttributeNS('base','q'),
'1')
d2 = microdom.parseString(s2)
self.assertEquals(d2.documentElement.namespace,
"base")
self.assertEquals(d2.documentElement.getElementsByTagName("y")[0].namespace,
"base")
self.assertEquals(
d2.documentElement.getElementsByTagName("y")[1].getAttributeNS('base','q'),
'1')
示例3: setAuthors
def setAuthors(template, authors):
# First, similarly to setTitle, insert text into an <div class="authors">
text = ''
for name, href in authors:
# FIXME: Do proper quoting/escaping (is it ok to use
# xml.sax.saxutils.{escape,quoteattr}?)
anchor = '<a href="%s">%s</a>' % (href, name)
if (name, href) == authors[-1]:
if len(authors) == 1:
text = anchor
else:
text += 'and ' + anchor
else:
text += anchor + ','
childNodes = microdom.parseString('<span>' + text +'</span>').childNodes
for node in domhelpers.findElementsWithAttribute(template,
"class", 'authors'):
node.childNodes.extend(childNodes)
# Second, add appropriate <link rel="author" ...> tags to the <head>.
head = domhelpers.findNodesNamed(template, 'head')[0]
authors = [microdom.parseString('<link rel="author" href="%s" title="%s"/>'
% (href, name)).childNodes[0]
for name, href in authors]
head.childNodes.extend(authors)
示例4: generateToC
def generateToC(document):
"""
Create a table of contents for the given document.
@type document: A DOM Node or Document
@rtype: A DOM Node
@return: a Node containing a table of contents based on the headers of the
given document.
"""
toc, level, id = "\n<ol>\n", 0, 0
for element in getHeaders(document):
elementLevel = int(element.tagName[1]) - 2
toc += (level - elementLevel) * "</ul>\n"
toc += (elementLevel - level) * "<ul>"
toc += '<li><a href="#auto%d">' % id
toc += domhelpers.getNodeText(element)
toc += "</a></li>\n"
level = elementLevel
anchor = microdom.parseString('<a name="auto%d" />' % id).documentElement
element.childNodes.append(anchor)
id += 1
toc += "</ul>\n" * level
toc += "</ol>\n"
return microdom.parseString(toc).documentElement
示例5: testSearch
def testSearch(self):
s = "<foo><bar id='me' /><baz><foo /></baz></foo>"
s2 = "<fOo><bAr id='me' /><bAz><fOO /></bAz></fOo>"
d = microdom.parseString(s)
d2 = microdom.parseString(s2, caseInsensitive=0, preserveCase=1)
d3 = microdom.parseString(s2, caseInsensitive=1, preserveCase=1)
root = d.documentElement
self.assertEquals(root.firstChild(), d.getElementById('me'))
self.assertEquals(d.getElementsByTagName("foo"),
[root, root.lastChild().firstChild()])
root = d2.documentElement
self.assertEquals(root.firstChild(), d2.getElementById('me'))
self.assertEquals(d2.getElementsByTagName('fOo'), [root])
self.assertEquals(d2.getElementsByTagName('fOO'),
[root.lastChild().firstChild()])
self.assertEquals(d2.getElementsByTagName('foo'), [])
root = d3.documentElement
self.assertEquals(root.firstChild(), d3.getElementById('me'))
self.assertEquals(d3.getElementsByTagName('FOO'),
[root, root.lastChild().firstChild()])
self.assertEquals(d3.getElementsByTagName('fOo'),
[root, root.lastChild().firstChild()])
示例6: testMutate
def testMutate(self):
s = "<foo />"
s1 = '<foo a="b"><bar/><foo/></foo>'
s2 = '<foo a="b">foo</foo>'
d = microdom.parseString(s).documentElement
d1 = microdom.parseString(s1).documentElement
d2 = microdom.parseString(s2).documentElement
d.appendChild(d.cloneNode())
d.setAttribute("a", "b")
child = d.childNodes[0]
self.assertEquals(child.getAttribute("a"), None)
self.assertEquals(child.nodeName, "foo")
d.insertBefore(microdom.Element("bar"), child)
self.assertEquals(d.childNodes[0].nodeName, "bar")
self.assertEquals(d.childNodes[1], child)
for n in d.childNodes:
self.assertEquals(n.parentNode, d)
self.assert_(d.isEqualToNode(d1))
d.removeChild(child)
self.assertEquals(len(d.childNodes), 1)
self.assertEquals(d.childNodes[0].nodeName, "bar")
t = microdom.Text("foo")
d.replaceChild(t, d.firstChild())
self.assertEquals(d.firstChild(), t)
self.assert_(d.isEqualToNode(d2))
示例7: testOutput
def testOutput(self):
for s, out in self.samples:
d = microdom.parseString(s, caseInsensitive=0)
d2 = microdom.parseString(out, caseInsensitive=0)
testOut = d.documentElement.toxml()
self.assertEquals(out, testOut)
self.assert_(d.isEqualToDocument(d2))
示例8: testEatingWhitespace
def testEatingWhitespace(self):
s = """<hello>
</hello>"""
d = microdom.parseString(s)
self.failUnless(not d.documentElement.hasChildNodes(),
d.documentElement.childNodes)
self.failUnless(d.isEqualToDocument(microdom.parseString('<hello></hello>')))
示例9: testLenientAmpersand
def testLenientAmpersand(self):
prefix = "<?xml version='1.0'?>"
# we use <pre> so space will be preserved
for i, o in [("&", "&"), ("& ", "& "), ("&", "&"), ("&hello monkey", "&hello monkey")]:
d = microdom.parseString("%s<pre>%s</pre>" % (prefix, i), beExtremelyLenient=1)
self.assertEqual(d.documentElement.toxml(), "<pre>%s</pre>" % o)
# non-space preserving
d = microdom.parseString("<t>hello & there</t>", beExtremelyLenient=1)
self.assertEqual(d.documentElement.toxml(), "<t>hello & there</t>")
示例10: testDoctype
def testDoctype(self):
s = '<?xml version="1.0"?>' '<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">' "<foo></foo>"
s2 = "<foo/>"
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEqual(d.doctype, 'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEqual(d.toxml(), s)
self.failIf(d.isEqualToDocument(d2))
self.failUnless(d.documentElement.isEqualToNode(d2.documentElement))
示例11: testSingletons
def testSingletons(self):
s = "<foo><b/><b /><b\n/></foo>"
s2 = "<foo><b/><b/><b/></foo>"
nodes = microdom.parseString(s).documentElement.childNodes
nodes2 = microdom.parseString(s2).documentElement.childNodes
self.assertEquals(len(nodes), 3)
for (n, n2) in zip(nodes, nodes2):
self.assert_(isinstance(n, microdom.Element))
self.assertEquals(n.nodeName, "b")
self.assert_(n.isEqualToNode(n2))
示例12: test_replaceNonChild
def test_replaceNonChild(self):
"""
L{Node.replaceChild} raises L{ValueError} if the node given to be
replaced is not a child of the node C{replaceChild} is called on.
"""
parent = microdom.parseString("<foo />")
orphan = microdom.parseString("<bar />")
replacement = microdom.parseString("<baz />")
self.assertRaises(ValueError, parent.replaceChild, replacement, orphan)
示例13: test_getElementsByTagName
def test_getElementsByTagName(self):
doc1=microdom.parseString('<foo/>')
actual=domhelpers.getElementsByTagName(doc1, 'foo')[0].nodeName
expected='foo'
self.assertEquals(actual, expected)
el1=doc1.documentElement
actual=domhelpers.getElementsByTagName(el1, 'foo')[0].nodeName
self.assertEqual(actual, expected)
doc2_xml='<a><foo in="a"/><b><foo in="b"/></b><c><foo in="c"/></c><foo in="d"/><foo in="ef"/><g><foo in="g"/><h><foo in="h"/></h></g></a>'
doc2=microdom.parseString(doc2_xml)
tag_list=domhelpers.getElementsByTagName(doc2, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
expected='abcdefgh'
self.assertEquals(actual, expected)
el2=doc2.documentElement
tag_list=domhelpers.getElementsByTagName(el2, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
self.assertEqual(actual, expected)
doc3_xml='''
<a><foo in="a"/>
<b><foo in="b"/>
<d><foo in="d"/>
<g><foo in="g"/></g>
<h><foo in="h"/></h>
</d>
<e><foo in="e"/>
<i><foo in="i"/></i>
</e>
</b>
<c><foo in="c"/>
<f><foo in="f"/>
<j><foo in="j"/></j>
</f>
</c>
</a>'''
doc3=microdom.parseString(doc3_xml)
tag_list=domhelpers.getElementsByTagName(doc3, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
expected='abdgheicfj'
self.assertEquals(actual, expected)
el3=doc3.documentElement
tag_list=domhelpers.getElementsByTagName(el3, 'foo')
actual=''.join([node.getAttribute('in') for node in tag_list])
self.assertEqual(actual, expected)
doc4_xml='<foo><bar></bar><baz><foo/></baz></foo>'
doc4=microdom.parseString(doc4_xml)
actual=domhelpers.getElementsByTagName(doc4, 'foo')
root=doc4.documentElement
expected=[root, root.lastChild().firstChild()]
self.assertEquals(actual, expected)
actual=domhelpers.getElementsByTagName(root, 'foo')
self.assertEqual(actual, expected)
示例14: testDoctype
def testDoctype(self):
s = ('<?xml version="1.0"?>'
'<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">'
'<foo />')
s2 = '<foo/>'
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEquals(d.doctype, 'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEquals(d.toxml(), s)
self.failIfEqual(d, d2)
self.failUnlessEqual(d.documentElement, d2.documentElement)
示例15: test_doctype
def test_doctype(self):
s = ('<?xml version="1.0"?>'
'<!DOCTYPE foo PUBLIC "baz" "http://www.example.com/example.dtd">'
'<foo></foo>')
s2 = '<foo/>'
d = microdom.parseString(s)
d2 = microdom.parseString(s2)
self.assertEqual(d.doctype,
'foo PUBLIC "baz" "http://www.example.com/example.dtd"')
self.assertEqual(d.toxml(), s)
self.assertFalse(d.isEqualToDocument(d2))
self.assertTrue(d.documentElement.isEqualToNode(d2.documentElement))