本文整理汇总了Python中lxml.etree.ProcessingInstruction方法的典型用法代码示例。如果您正苦于以下问题:Python etree.ProcessingInstruction方法的具体用法?Python etree.ProcessingInstruction怎么用?Python etree.ProcessingInstruction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lxml.etree
的用法示例。
在下文中一共展示了etree.ProcessingInstruction方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: saxify
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def saxify(self):
self._content_handler.startDocument()
element = self._element
if hasattr(element, 'getprevious'):
siblings = []
sibling = element.getprevious()
while getattr(sibling, 'tag', None) is ProcessingInstruction:
siblings.append(sibling)
sibling = sibling.getprevious()
for sibling in siblings[::-1]:
self._recursive_saxify(sibling, {})
self._recursive_saxify(element, {})
if hasattr(element, 'getnext'):
sibling = element.getnext()
while getattr(sibling, 'tag', None) is ProcessingInstruction:
self._recursive_saxify(sibling, {})
sibling = sibling.getnext()
self._content_handler.endDocument()
示例2: processingInstruction
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def processingInstruction(self, target, data):
pi = ProcessingInstruction(target, data)
if self._root is None:
self._root_siblings.append(pi)
else:
self._element_stack[-1].append(pi)
示例3: _parse_element_r
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def _parse_element_r(self, el, specials, refs, id=None, element_cls=Paragraph):
"""Recursively parse HTML/XML element and its children into a list of Document elements."""
elements = []
if el.tag in {etree.Comment, etree.ProcessingInstruction}:
return []
# if el in refs:
# return [element_cls('', references=refs[el])]
if el in specials:
return specials[el]
id = el.get('id', id)
references = refs.get(el, [])
if el.text is not None:
elements.append(element_cls(six.text_type(el.text), id=id, references=references))
elif references:
elements.append(element_cls('', id=id, references=references))
for child in el:
# br is a special case - technically inline, but we want to split
if child.tag not in {etree.Comment, etree.ProcessingInstruction} and child.tag.lower() == 'br':
elements.append(element_cls(''))
child_elements = self._parse_element_r(child, specials=specials, refs=refs, id=id, element_cls=element_cls)
if (self._is_inline(child) and len(elements) > 0 and len(child_elements) > 0 and
isinstance(elements[-1], (Text, Sentence)) and isinstance(child_elements[0], (Text, Sentence)) and
type(elements[-1]) == type(child_elements[0])):
elements[-1] += child_elements.pop(0)
elements.extend(child_elements)
if child.tail is not None:
if self._is_inline(child) and len(elements) > 0 and isinstance(elements[-1], element_cls):
elements[-1] += element_cls(six.text_type(child.tail), id=id)
else:
elements.append(element_cls(six.text_type(child.tail), id=id))
return elements
示例4: _is_inline
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def _is_inline(self, element):
"""Return True if an element is inline."""
if element.tag not in {etree.Comment, etree.ProcessingInstruction} and element.tag.lower() in self.inline_elements:
return True
return False
示例5: _recursive_saxify
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def _recursive_saxify(self, element, prefixes):
content_handler = self._content_handler
tag = element.tag
if tag is Comment or tag is ProcessingInstruction:
if tag is ProcessingInstruction:
content_handler.processingInstruction(
element.target, element.text)
if element.tail:
content_handler.characters(element.tail)
return
new_prefixes = []
build_qname = self._build_qname
attribs = element.items()
if attribs:
attr_values = {}
attr_qnames = {}
for attr_ns_name, value in attribs:
attr_ns_tuple = _getNsTag(attr_ns_name)
attr_values[attr_ns_tuple] = value
attr_qnames[attr_ns_tuple] = build_qname(
attr_ns_tuple[0], attr_ns_tuple[1], prefixes, new_prefixes)
sax_attributes = self._attr_class(attr_values, attr_qnames)
else:
sax_attributes = self._empty_attributes
ns_uri, local_name = _getNsTag(tag)
qname = build_qname(ns_uri, local_name, prefixes, new_prefixes)
for prefix, uri in new_prefixes:
content_handler.startPrefixMapping(prefix, uri)
content_handler.startElementNS((ns_uri, local_name),
qname, sax_attributes)
if element.text:
content_handler.characters(element.text)
for child in element:
self._recursive_saxify(child, prefixes)
content_handler.endElementNS((ns_uri, local_name), qname)
for prefix, uri in new_prefixes:
content_handler.endPrefixMapping(prefix)
if element.tail:
content_handler.characters(element.tail)
示例6: _recursive_saxify
# 需要导入模块: from lxml import etree [as 别名]
# 或者: from lxml.etree import ProcessingInstruction [as 别名]
def _recursive_saxify(self, element, parent_nsmap):
content_handler = self._content_handler
tag = element.tag
if tag is Comment or tag is ProcessingInstruction:
if tag is ProcessingInstruction:
content_handler.processingInstruction(
element.target, element.text)
tail = element.tail
if tail:
content_handler.characters(tail)
return
element_nsmap = element.nsmap
new_prefixes = []
if element_nsmap != parent_nsmap:
# There have been updates to the namespace
for prefix, ns_uri in element_nsmap.items():
if parent_nsmap.get(prefix) != ns_uri:
new_prefixes.append( (prefix, ns_uri) )
attribs = element.items()
if attribs:
attr_values = {}
attr_qnames = {}
for attr_ns_name, value in attribs:
attr_ns_tuple = _getNsTag(attr_ns_name)
attr_values[attr_ns_tuple] = value
attr_qnames[attr_ns_tuple] = self._build_qname(
attr_ns_tuple[0], attr_ns_tuple[1], element_nsmap,
preferred_prefix=None, is_attribute=True)
sax_attributes = self._attr_class(attr_values, attr_qnames)
else:
sax_attributes = self._empty_attributes
ns_uri, local_name = _getNsTag(tag)
qname = self._build_qname(
ns_uri, local_name, element_nsmap, element.prefix, is_attribute=False)
for prefix, uri in new_prefixes:
content_handler.startPrefixMapping(prefix, uri)
content_handler.startElementNS(
(ns_uri, local_name), qname, sax_attributes)
text = element.text
if text:
content_handler.characters(text)
for child in element:
self._recursive_saxify(child, element_nsmap)
content_handler.endElementNS((ns_uri, local_name), qname)
for prefix, uri in new_prefixes:
content_handler.endPrefixMapping(prefix)
tail = element.tail
if tail:
content_handler.characters(tail)