本文整理汇总了Python中xml.dom.minidom.Element方法的典型用法代码示例。如果您正苦于以下问题:Python minidom.Element方法的具体用法?Python minidom.Element怎么用?Python minidom.Element使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.dom.minidom
的用法示例。
在下文中一共展示了minidom.Element方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cleanXml
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def cleanXml(node):
## remove extraneous text; let the xml library do the formatting.
hasElement = False
nonElement = []
for ch in node.childNodes:
if isinstance(ch, xml.Element):
hasElement = True
cleanXml(ch)
else:
nonElement.append(ch)
if hasElement:
for ch in nonElement:
node.removeChild(ch)
elif node.tagName == 'g': ## remove childless groups
node.parentNode.removeChild(node)
示例2: render_GET
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def render_GET(self, request):
"""
Render as HTML a listing of all known users with links to their
personal resources.
"""
listing = Element('ul')
for link, text in self._users():
linkElement = Element('a')
linkElement.setAttribute('href', link + '/')
textNode = Text()
textNode.data = text
linkElement.appendChild(textNode)
item = Element('li')
item.appendChild(linkElement)
listing.appendChild(item)
return self.template % {'users': listing.toxml()}
示例3: get_inline_style
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def get_inline_style(node: minidom.Element, base_style: dict = None) -> dict:
""" update the basestyle with the style defined by the style property of the node """
style = {}
if base_style is not None:
style.update(base_style)
attribute_names = ["alignment-baseline", "baseline-shift", "clip-path", "clip-rule", "color", "color-interpolation", "color-interpolation-filters", "color-rendering", "cursor", "direction", "display", "dominant-baseline", "fill", "fill-opacity", "fill-rule", "filter", "flood-color", "flood-opacity", "font-family", "font-size", "font-size-adjust", "font-stretch", "font-style", "font-variant", "font-weight", "glyph-orientation-horizontal", "glyph-orientation-vertical", "image-rendering", "letter-spacing", "lighting-color", "marker-end", "marker-mid", "marker-start", "mask", "opacity", "overflow", "paint-order", "pointer-events", "shape-rendering", "stop-color", "stop-opacity", "stroke", "stroke-dasharray", "stroke-dashoffset", "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", "stroke-width", "text-anchor", "text-decoration", "text-overflow", "text-rendering", "unicode-bidi", "vector-effect", "visibility", "white-space", "word-spacing", "writing-mode"]
for name in attribute_names:
value = node.getAttribute(name)
if value != "":
style[name] = value
for element in node.getAttribute("style").split(";"):
if element == "":
continue
key, value = element.split(":", 1)
style[key] = value
return style
示例4: get_css_style
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def get_css_style(node: minidom.Element, css_list: list, base_style: dict) -> dict:
""" update the base_style with the style definitions from the stylesheet that are applicable to the node
defined by the classes or id of the node
"""
style = {}
if base_style is not None:
style.update(base_style)
classes = node.getAttribute("class").split()
for css in css_list:
css_condition, css_style = css
if css_condition[0] == "." and css_condition[1:] in classes:
style.update(css_style)
elif css_condition[0] == "#" and css_condition[1:] == node.getAttribute("id"):
style.update(css_style)
elif css_condition == node.tagName:
style.update(css_style)
return style
示例5: plt_patch
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def plt_patch(node: minidom.Element, trans_parent_trans: mtransforms.Transform, style: dict, constructor: callable, ids: dict, no_draw: bool = False) -> mpatches.Patch:
""" add a node to the figure by calling the provided constructor """
trans_node = parseTransformation(node.getAttribute("transform"))
style = get_inline_style(node, get_css_style(node, ids["css"], style))
patch = constructor(node, trans_node + trans_parent_trans + plt.gca().transData, style, ids)
if not isinstance(patch, list):
patch = [patch]
for p in patch:
if not getattr(p, "is_marker", False):
style = apply_style(style, p)
p.style = style
#p.set_transform(p.get_transform() + plt.gca().transData)
p.trans_parent = trans_parent_trans
p.trans_node = parseTransformation(node.getAttribute("transform"))
if not no_draw and not styleNoDisplay(style):
plt.gca().add_patch(p)
if node.getAttribute("id") != "":
ids[node.getAttribute("id")] = patch
return patch
示例6: _elementConstructor
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def _elementConstructor(self, tag_name, namespaceURI=None, prefix=None, localName=None):
return DOM.Element(tag_name,
namespaceURI,
self.getPrefix(tag_name),
self.getLocalName(tag_name))
# _localName = self.getLocalName(tag_name)
# element = DOM.Element(tag_name, namespaceURI, prefix, _localName)
#
# prefix = self.getPrefix(tag_name)
# element.prefix = prefix
#
# return element
# element = DOM.Element(tag_name, namespaceURI, prefix, localName)
# prefix = self.getPrefix(tag_name)
# localName = self.getLocalName(tag_name)
# element.prefix = prefix
# element.localName = localName
# return element
示例7: _element2dict
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def _element2dict(self, parent):
"""
将单个节点转换为 dict
"""
d = {}
for node in parent.childNodes:
if not isinstance(node, minidom.Element):
continue
if not node.hasChildNodes():
continue
if node.childNodes[0].nodeType == minidom.Node.ELEMENT_NODE:
try:
d[node.tagName]
except KeyError:
d[node.tagName] = []
d[node.tagName].append(self._element2dict(node))
elif len(node.childNodes) == 1 and node.childNodes[0].nodeType in [minidom.Node.CDATA_SECTION_NODE, minidom.Node.TEXT_NODE]:
d[node.tagName] = node.childNodes[0].data
return d
示例8: _makeLineNumbers
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def _makeLineNumbers(howMany):
"""
Return an element which will render line numbers for a source listing.
@param howMany: The number of lines in the source listing.
@type howMany: C{int}
@return: An L{dom.Element} which can be added to the document before
the source listing to add line numbers to it.
"""
# Figure out how many digits wide the widest line number label will be.
width = len(str(howMany))
# Render all the line labels with appropriate padding
labels = ['%*d' % (width, i) for i in range(1, howMany + 1)]
# Create a p element with the right style containing the labels
p = dom.Element('p')
p.setAttribute('class', 'py-linenumber')
t = dom.Text()
t.data = '\n'.join(labels) + '\n'
p.appendChild(t)
return p
示例9: findNodeJustBefore
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def findNodeJustBefore(target, nodes):
"""
Find the last Element which is a sibling of C{target} and is in C{nodes}.
@param target: A node the previous sibling of which to return.
@param nodes: A list of nodes which might be the right node.
@return: The previous sibling of C{target}.
"""
while target is not None:
node = target.previousSibling
while node is not None:
if node in nodes:
return node
node = node.previousSibling
target = target.parentNode
raise RuntimeError("Oops")
示例10: test_head
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_head(self):
"""
L{LatexSpitter.visitNode} writes out author information for each
I{link} element with a I{rel} attribute set to I{author}.
"""
head = Element('head')
first = Element('link')
first.setAttribute('rel', 'author')
first.setAttribute('title', 'alice')
second = Element('link')
second.setAttribute('rel', 'author')
second.setAttribute('href', 'http://example.com/bob')
third = Element('link')
third.setAttribute('rel', 'author')
third.setAttribute('href', 'mailto:carol@example.com')
head.appendChild(first)
head.appendChild(second)
head.appendChild(third)
self.spitter.visitNode(head)
self.assertEqual(
''.join(self.output),
'\\author{alice \\and $<$http://example.com/bob$>$ \\and $<$carol@example.com$>$}')
示例11: test_anchorListing
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_anchorListing(self):
"""
L{LatexSpitter.visitNode} emits a verbatim block when it encounters a
code listing (represented by an I{a} element with a I{listing} class).
"""
path = FilePath(self.mktemp())
path.setContent('foo\nbar\n')
listing = Element('a')
listing.setAttribute('class', 'listing')
listing.setAttribute('href', path.path)
self.spitter.visitNode(listing)
self.assertEqual(
''.join(self.output),
"\\begin{verbatim}\n"
"foo\n"
"bar\n"
"\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
"\\begin{em}temp\\end{em}\\end{center}}")
示例12: test_anchorListingSkipLines
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_anchorListingSkipLines(self):
"""
When passed an I{a} element with a I{listing} class and an I{skipLines}
attribute, L{LatexSpitter.visitNode} emits a verbatim block which skips
the indicated number of lines from the beginning of the source listing.
"""
path = FilePath(self.mktemp())
path.setContent('foo\nbar\n')
listing = Element('a')
listing.setAttribute('class', 'listing')
listing.setAttribute('skipLines', '1')
listing.setAttribute('href', path.path)
self.spitter.visitNode(listing)
self.assertEqual(
''.join(self.output),
"\\begin{verbatim}\n"
"bar\n"
"\\end{verbatim}\\parbox[b]{\\linewidth}{\\begin{center} --- "
"\\begin{em}temp\\end{em}\\end{center}}")
示例13: test_setTitle
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_setTitle(self):
"""
L{tree.setTitle} inserts the given title into the first I{title}
element and the first element with the I{title} class in the given
template.
"""
parent = dom.Element('div')
firstTitle = dom.Element('title')
parent.appendChild(firstTitle)
secondTitle = dom.Element('span')
secondTitle.setAttribute('class', 'title')
parent.appendChild(secondTitle)
titleNodes = [dom.Text()]
# minidom has issues with cloning documentless-nodes. See Python issue
# 4851.
titleNodes[0].ownerDocument = dom.Document()
titleNodes[0].data = 'foo bar'
tree.setTitle(parent, titleNodes, None)
self.assertEqual(firstTitle.toxml(), '<title>foo bar</title>')
self.assertEqual(
secondTitle.toxml(), '<span class="title">foo bar</span>')
示例14: test_setTitleWithChapter
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_setTitleWithChapter(self):
"""
L{tree.setTitle} includes a chapter number if it is passed one.
"""
document = dom.Document()
parent = dom.Element('div')
parent.ownerDocument = document
title = dom.Element('title')
parent.appendChild(title)
titleNodes = [dom.Text()]
titleNodes[0].ownerDocument = document
titleNodes[0].data = 'foo bar'
# Oh yea. The numberer has to agree to put the chapter number in, too.
numberer.setNumberSections(True)
tree.setTitle(parent, titleNodes, '13')
self.assertEqual(title.toxml(), '<title>13. foo bar</title>')
示例15: test_addMtime
# 需要导入模块: from xml.dom import minidom [as 别名]
# 或者: from xml.dom.minidom import Element [as 别名]
def test_addMtime(self):
"""
L{tree.addMtime} inserts a text node giving the last modification time
of the specified file wherever it encounters an element with the
I{mtime} class.
"""
path = FilePath(self.mktemp())
path.setContent('')
when = time.ctime(path.getModificationTime())
parent = dom.Element('div')
mtime = dom.Element('span')
mtime.setAttribute('class', 'mtime')
parent.appendChild(mtime)
tree.addMtime(parent, path.path)
self.assertEqual(
mtime.toxml(), '<span class="mtime">' + when + '</span>')