本文整理汇总了Python中meresco.components.lxmltostring函数的典型用法代码示例。如果您正苦于以下问题:Python lxmltostring函数的具体用法?Python lxmltostring怎么用?Python lxmltostring使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了lxmltostring函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: html_to_etree
def html_to_etree(in_str, remove_blank_text=True):
"""
Parses a tree of possibly malformed HTML5, according to WHATWG HTML5 rules.
Result is either:
- parsed input, or;
- if multiple fragments (> 1 top-level tags) are given: parsed input wrapped in either a `div' or `span', or;
- None for empty input.
"""
if in_str is None:
return None
if not isinstance(in_str, basestring):
raise ValueError('input must be a string')
in_str = _nfc(in_str).strip()
if not in_str:
return None
# Double-parse to remove (hopefully irrelevant) whitespace - some not-so-irrelevant whitespace will most likely be removed too
etree = fromstring(in_str, parser=_html5Parser) # ATTENTION: tag/attributes namespace-info mangled here due to html5lib bugs.
_etree_mutate_fix_localname(etree)
if remove_blank_text:
s = lxmltostring(etree)
etree = parse(StringIO(s), parser=_xmlParser)
etree = fromstring(lxmltostring(etree), parser=_html5Parser)
_etree_mutate_fix_localname(etree) # and they spawn again after fromstring, so remove them again.
return etree.getroot() if hasattr(etree, 'getroot') else etree
示例2: testQueryTimeInExtraResponse
def testQueryTimeInExtraResponse(self):
handler = SruHandler(includeQueryTimes=True)
observer = CallTrace('observer', emptyGeneratorMethods=['echoedExtraRequestData', 'extraResponseData'])
times = [1, 2.5, 3.5]
def timeNow():
return times.pop(0)
handler._timeNow = timeNow
def executeQuery(**kwargs):
response = Response(total=0, hits=[])
response.queryTime=5
raise StopIteration(response)
yield
observer.methods['executeQuery'] = executeQuery
handler.addObserver(observer)
arguments = dict(startRecord=11, maximumRecords=15, query='query', recordPacking='string', recordSchema='schema')
result = "".join(compose(handler.searchRetrieve(sruArguments=arguments, **arguments)))
sruResponse = parse(StringIO(result))
extraResponseData = sruResponse.xpath('/srw:searchRetrieveResponse/srw:extraResponseData', namespaces={'srw':"http://www.loc.gov/zing/srw/"})[0]
self.assertEqualsWS("""<srw:extraResponseData %(xmlns_srw)s %(xmlns_diag)s %(xmlns_xcql)s %(xmlns_dc)s %(xmlns_meresco_srw)s>
<querytimes xmlns="http://meresco.org/namespace/timing">
<sruHandling>PT2.500S</sruHandling>
<sruQueryTime>PT1.500S</sruQueryTime>
<index>PT0.005S</index>
</querytimes>
</srw:extraResponseData>""" % namespaces, lxmltostring(extraResponseData))
queryTimes = lxmltostring(extraResponseData.xpath('//ti:querytimes', namespaces={'ti':"http://meresco.org/namespace/timing"})[0])
assertValid(queryTimes, join(schemasPath, 'timing-20120827.xsd'))
self.assertEquals(['executeQuery', 'echoedExtraRequestData', 'extraResponseData', 'handleQueryTimes'], observer.calledMethodNames())
self.assertEquals({'sru': Decimal("2.500"), 'queryTime': Decimal("1.500"), 'index': Decimal("0.005")}, observer.calledMethods[3].kwargs)
示例3: testListMetadataFormatsForIdentifier
def testListMetadataFormatsForIdentifier(self):
header, body = self._request(verb=['ListMetadataFormats'], identifier=[self.prefix + 'record:id:01'])
self.assertEquals(0, len(xpath(body, '/oai:OAI-PMH/oai:error')), lxmltostring(body, pretty_print=True))
formats = xpath(body, '/oai:OAI-PMH/oai:ListMetadataFormats/oai:metadataFormat')
self.assertEquals(1, len(formats), lxmltostring(body, pretty_print=True))
self.assertEquals(['oai_dc'], xpath(formats[0], 'oai:metadataPrefix/text()'))
示例4: testTailTakenCareOfWithoutAffectingOriginal
def testTailTakenCareOfWithoutAffectingOriginal(self):
observer = CallTrace('observer', methods={'test': lambda *args, **kwargs: (x for x in [])})
observable = be(
(Observable(),
(XmlXPath(
['/myns:root/myns:path'],
fromKwarg='lxmlNode',
namespaces={'myns': 'http://myns.org/'}
),
(observer, ),
)
)
)
XML = """\
<root xmlns:myns="http://myns.org/" xmlns="http://myns.org/">
<myns:path>
<to>me</to>
</myns:path>\n
</root>"""
lxmlNode = parse(StringIO(XML))
self.assertEquals(XML, lxmltostring(lxmlNode))
list(compose(observable.all.test('een tekst', lxmlNode=lxmlNode)))
self.assertEquals(1, len(observer.calledMethods))
method = observer.calledMethods[0]
self.assertEquals('test', method.name)
self.assertEqualsWS('<myns:path xmlns:myns="http://myns.org/" xmlns="http://myns.org/"><to>me</to></myns:path>', lxmltostring(method.kwargs['lxmlNode']))
self.assertEquals("""\
<myns:path xmlns:myns="http://myns.org/" xmlns="http://myns.org/">
<to>me</to>
</myns:path>""", lxmltostring(method.kwargs['lxmlNode']))
self.assertEquals(XML, lxmltostring(lxmlNode))
示例5: assertWaterMarked
def assertWaterMarked(**oaiArgs):
header, body = self._request(**oaiArgs)
try:
comment = xpath(body, "/oai:OAI-PMH/comment()")[0]
except:
print lxmltostring(body, pretty_print=True)
raise
self.assertEquals(" Watermarked by Seecr ", comment.text)
示例6: testNoLxmlTailOnPart
def testNoLxmlTailOnPart(self):
inputEvent = fromstring("""<document><part name="partone"><some>message</some>\n\n\n\n</part><part name="parttwo"><second>message</second>\n\n\n\n</part></document>""")
interceptor = CallTrace('Interceptor', methods={'add': yieldNothing})
v = createVenturiHelix([{'partname': 'partone', 'xpath': '/document/part[@name="partone"]/text()'}, {'partname': 'parttwo', 'xpath': '/document/part/second'}], [], interceptor)
list(compose(v.all.add('identifier', 'document', inputEvent)))
self.assertEquals('<some>message</some>', lxmltostring(interceptor.calledMethods[1].kwargs['lxmlNode']))
secondXml = interceptor.calledMethods[2].kwargs['lxmlNode']
self.assertEquals('<second>message</second>', lxmltostring(secondXml))
示例7: testFindUsingMultipleXPaths
def testFindUsingMultipleXPaths(self):
self.createXmlXPath(['/does/not/exist', '/a/b', '/a/b/c'], {})
self.observable.do.test(data='<a><b><c>one</c></b><b><d>two</d></b></a>')
self.assertEquals(3, len(self.observer.calledMethods))
allResults = []
for method in self.observer.calledMethods:
allResults.append(method.kwargs['lxmlNode'])
self.assertEqualsWS('<b><c>one</c></b>', lxmltostring(allResults[0]))
self.assertEqualsWS('<b><d>two</d></b>', lxmltostring(allResults[1]))
self.assertEqualsWS('<c>one</c>', lxmltostring(allResults[2]))
示例8: testPartsWithUnicodeChars
def testPartsWithUnicodeChars(self):
inputEvent = fromstring("""<document><part name="partone"><some>t€xt</some>\n\n\n\n</part><part name="parttwo"><second>t€xt</second>\n\n\n\n</part></document>""")
interceptor = CallTrace('Interceptor', methods={'add': yieldNothing})
v = createVenturiHelix([{'partname': 'partone', 'xpath': '/document/part[@name="partone"]/text()'}, {'partname': 'parttwo', 'xpath': '/document/part/second'}], [], interceptor)
list(compose(v.all.add('identifier', 'document', inputEvent)))
firstXml = interceptor.calledMethods[1].kwargs['lxmlNode']
self.assertEquals('<some>t€xt</some>', lxmltostring(firstXml))
self.assertEquals('t€xt', firstXml.getroot().text)
secondXml = interceptor.calledMethods[2].kwargs['lxmlNode']
self.assertEquals('<second>t€xt</second>', lxmltostring(secondXml))
self.assertEquals('t€xt', secondXml.getroot().text)
示例9: testSimpleXPath
def testSimpleXPath(self):
self.createXmlXPath(['/root/path'], {})
xml = '<root><path><to>me</to></path>\n</root>'
self.observable.do.test('een tekst', data=xml)
self.assertEquals(1, len(self.observer.calledMethods))
method = self.observer.calledMethods[0]
self.assertEquals('test', method.name)
self.assertEquals(1, len(method.args))
self.assertEquals('een tekst', method.args[0])
self.assertEqualsWS('<path><to>me</to></path>', lxmltostring(method.kwargs['lxmlNode']))
self.assertEquals('<path><to>me</to></path>', lxmltostring(method.kwargs['lxmlNode']))
示例10: testXPathWithMultipleResults
def testXPathWithMultipleResults(self):
self.createXmlXPath(['/root/element/data'], {})
self.observable.do.aMethod(data="""<root>
<element>
<data>one</data>
</element>
<element>
<data>two</data>
</element>
</root>""")
self.assertEquals(2, len(self.observer.calledMethods))
self.assertEqualsWS('<data>one</data>', lxmltostring(self.observer.calledMethods[0].kwargs['lxmlNode']))
self.assertEqualsWS('<data>two</data>', lxmltostring(self.observer.calledMethods[1].kwargs['lxmlNode']))
示例11: testTestWithCondition
def testTestWithCondition(self):
self.createXmlXPath(['/a/*[not(self::b) and not(self::c)]'], {})
self.observable.do.test(data='<a><b>zero</b><c>one</c><d>two</d></a>')
self.assertEquals(1, len(self.observer.calledMethods))
self.assertEqualsWS('<d>two</d>', lxmltostring(self.observer.calledMethods[0].kwargs['lxmlNode']))
示例12: testListRecords
def testListRecords(self):
header, body = self._request(
verb=['ListRecords'], metadataPrefix=['prefix2'])
records = xpath(body, '/oai:OAI-PMH/oai:ListRecords/oai:record')
self.assertEquals(10, len(records))
self.assertEquals([self.prefix + 'record:id:11'],
xpath(records[1],
'oai:header/oai:identifier/text()'))
self.assertEquals(['record:id:11'],
xpath(records[1],
'oai:metadata/oai_dc:dc/dc:subject/text()'),
lxmltostring(records[1]))
self.assertEquals(['hierarchical', 'setSpec10'],
sorted(
xpath(records[1],
'oai:header/oai:setSpec/text()')))
deletedRecords = xpath(
body,
'/oai:OAI-PMH/oai:ListRecords/oai:record[oai:header/@status="deleted"]'
)
self.assertEquals(2, len(deletedRecords))
self.assertEquals(
[0, 0], [len(xpath(r, 'oai:metadata')) for r in deletedRecords])
self.assertEquals(['hierarchical', 'setSpec10'],
sorted(
xpath(deletedRecords[0],
'oai:header/oai:setSpec/text()')))
示例13: testOnlyPassPartsSpecified
def testOnlyPassPartsSpecified(self):
inputEvent = fromstring("""<document><part name="partone"><some>message</some></part><part name="parttwo"><second/></part></document>""")
interceptor = CallTrace('Interceptor', methods={'add': yieldNothing})
v = createVenturiHelix([{'partname': 'partone', 'xpath': '/document/part[@name="partone"]/text()'}], [], interceptor)
list(compose(v.all.add('identifier', 'document', inputEvent)))
self.assertEquals(['begin', 'add'], [m.name for m in interceptor.calledMethods])
self.assertEquals('<some>message</some>', lxmltostring(interceptor.calledMethods[1].kwargs['lxmlNode']))
示例14: testCouldHave
def testCouldHave(self):
inputEvent = fromstring('<document><one/></document>')
interceptor = CallTrace('Interceptor', ignoredAttributes=['getData', 'all_unknown', 'any_unknown', 'call_unknown'], methods={'add': yieldNothing})
v = createVenturiHelix([], [{'partname': 'one', 'xpath': '/document/one'}], interceptor)
list(compose(v.all.add('identifier', 'document', inputEvent)))
self.assertEquals(['begin', 'add'], [m.name for m in interceptor.calledMethods])
self.assertEquals('<one/>', lxmltostring(interceptor.calledMethods[1].kwargs['lxmlNode']))
示例15: testListMetadataFormatsForWrongIdentifier
def testListMetadataFormatsForWrongIdentifier(self):
header, body = self._request(
verb=['ListMetadataFormats'], identifier=['does:not:exist'])
self.assertEquals(['idDoesNotExist'],
xpath(body, '/oai:OAI-PMH/oai:error/@code'),
lxmltostring(body, pretty_print=True))