本文整理匯總了Python中lxml.etree.ElementBase方法的典型用法代碼示例。如果您正苦於以下問題:Python etree.ElementBase方法的具體用法?Python etree.ElementBase怎麽用?Python etree.ElementBase使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類lxml.etree
的用法示例。
在下文中一共展示了etree.ElementBase方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: insert_errors
# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import ElementBase [as 別名]
def insert_errors(
el,
errors,
form_id=None,
form_index=None,
error_class="error",
error_creator=default_error_creator,
):
el = _find_form(el, form_id=form_id, form_index=form_index)
for name, error in errors.items():
if error is None:
continue
for error_el, message in _find_elements_for_name(el, name, error):
assert isinstance(message, (basestring, type(None), ElementBase)), (
"Bad message: %r" % message)
_insert_error(error_el, message, error_class, error_creator)
示例2: __call__
# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import ElementBase [as 別名]
def __call__(self, el, is_block, message):
error_el = el.makeelement(self.error_container_tag)
if self.error_message_class:
error_el.set('class', self.error_message_class)
if is_block and self.error_block_class:
error_el.set('class', error_el.get('class', '')+' '+self.error_block_class)
if message is None or message == '':
message = self.default_message
if isinstance(message, ElementBase):
error_el.append(message)
else:
assert isinstance(message, basestring), (
"Bad message; should be a string or element: %r" % message)
error_el.text = message or self.default_message
if is_block and self.block_inside:
if self.insert_before:
error_el.tail = el.text
el.text = None
el.insert(0, error_el)
else:
el.append(error_el)
else:
parent = el.getparent()
pos = parent.index(el)
if self.insert_before:
parent.insert(pos, error_el)
else:
error_el.tail = el.tail
el.tail = None
parent.insert(pos+1, error_el)
示例3: GenerateAnyInfos
# 需要導入模塊: from lxml import etree [as 別名]
# 或者: from lxml.etree import ElementBase [as 別名]
def GenerateAnyInfos(infos):
def GetTextElement(tree):
if infos["namespace"][0] == "##any":
return tree.xpath("p")[0]
return tree.xpath("ns:p", namespaces={"ns": infos["namespace"][0]})[0]
def ExtractAny(tree):
return GetTextElement(tree).text
def GenerateAny(tree, value):
GetTextElement(tree).text = etree.CDATA(value)
def InitialAny():
if infos["namespace"][0] == "##any":
element_name = "p"
else:
element_name = "{%s}p" % infos["namespace"][0]
p = etree.Element(element_name)
p.text = etree.CDATA("")
return p
return {
"type": COMPLEXTYPE,
"extract": ExtractAny,
"generate": GenerateAny,
"initial": InitialAny,
"check": lambda x: isinstance(x, (string_types, etree.ElementBase))
}