def __fetchInheritedValue(lineage, property, searchpath): # Attempt to open every xml file in the search path, yuck, and if the # file contains a list of objects, then search that file for the current # class. for c in lineage: for p in searchpath: if (os.path.isdir(p)): for d in os.listdir(p): (f, ext) = os.path.splitext(p) if ext in (".xml"): dom = parse(d) ## we have a parse dom, check to see if the file entity is objects node = xpath.find("/objects/object[id='" + c + "']/property[id='" + property + "']") if node != None: # Found the class and property we are looking for so return the value # The value of the node should be its text node return node.getTextNode() else: # not a directory, just a file dom = parse(p) ## we have a parse dom, check to see if the file entity is objects node = xpath.find("/objects/object[id='" + c + "']/property[id='" + property + "']") if node != None: # Found the class and property we are looking for so return the value # The value of the node should be its text node return node.getTextNode() log.debug("Inherited property value request, none found") return None
def __addSessionToCommandFile(self, nBI, sessionID, initDate): try: doc = parse(self.commandsFilePath) node = xpath.find("/commandsLog/user[@nBI='" + nBI + "']", doc) if len(node) > 0: userElement = node[0] sessionElement = doc.createElement("session") userElement.appendChild(sessionElement) sessionElement.setAttribute("sessionID", sessionID) sessionElement.setAttribute("sessionStart", initDate) sessionElement.setAttribute("sessionEnd", "") else: topElement = xpath.find("/commandsLog", doc)[0] newElement = doc.createElement("user") topElement.appendChild(newElement) newElement.setAttribute("nBI", nBI) sessionElement = doc.createElement("session") newElement.appendChild(sessionElement) sessionElement.setAttribute("sessionID", sessionID) sessionElement.setAttribute("sessionStart", initDate) sessionElement.setAttribute("sessionEnd", "") self.__writeToXMLFile(doc) except: raise Exception("It was not possible to add the user to de command file")
def gera_vereadores(start, end, num): for i in range(start, end): vereador = vereadores[i] elem_vereador = elem_vereadores[i - start] # troca foto elem_foto = xpath.find("./*[@inkscape:label='foto']", elem_vereador)[0] elem_foto.setAttribute("xlink:href", openBase64(vereador["local_img"])) # troca nome elem_nome = xpath.find("./*[@inkscape:label='nome']/tspan/text()",elem_vereador)[0] elem_nome.nodeValue = vereador["nome"] # troca partido elem_partido = xpath.find("./*[@inkscape:label='partido']", elem_vereador)[0] elem_partido.setAttribute("xlink:href", openBase64(partidos[vereador["partido"]])) # troca sala elem_sala = xpath.find("./*[@inkscape:label='texto_sala']/tspan/text()", elem_vereador)[0] try: if vereador["sala"]: elem_sala.nodeValue = str(vereador["sala"]) except KeyError: elem_sala.parentNode.parentNode.parentNode.removeChild(elem_sala.parentNode.parentNode) elem_sala_caption = xpath.find("./*[@inkscape:label='texto_sala_caption']", elem_vereador)[0] elem_sala_caption.parentNode.removeChild(elem_sala_caption) with open(f'output/cartela_{num}.svg', 'w', encoding='utf-8') as f: cartela.writexml(f)
def refresh_movie_source(request): if 'source_key' not in request.REQUEST: return HttpResponseServerError( 'No source key specified in request params') source = models.MovieListingSource.get(request.REQUEST['source_key']) if not source: logging.error('Unable to find MovieListingSource: %s', request.REQUEST['source_key']) return HttpResponse('Error unable to find Source') elif not source.yql: logging.error('No yql for MovieListingSource: %s' % str(source)) return HttpResponse('No YQL for source') elif not source.settings: logging.error('No settings for MovieListingSource: %s' % str(source)) return HttpResponse('No settings for source') logging.info('Refreshing movie from source %s', str(source)) yql = source.yql if 'offset' in request.REQUEST: query_offset = int(request.REQUEST['offset']) + 1 yql = '%s limit %d offset %d' % ( yql, settings.MOVIE_REFRESH_QUERY_SIZE, query_offset) form_data = urllib.urlencode({ "q": yql, "format": "xml", "diagnostics": "false" }) result = urlfetch.fetch(url=settings.YQL_BASE_URL, payload=form_data, method=urlfetch.POST) dom = minidom.parseString(result.content) result_nodes = dom.getElementsByTagName('results')[0].childNodes name_nodes = xpath.find(source.settings.name_xpath, dom) leaches_nodes = xpath.find(source.settings.leaches_xpath, dom) logging.info('Found %d raw names', len(name_nodes)) strip_white_pattern = re.compile(r"\s+") source_results = [] for index, name_node in enumerate(name_nodes): logging.debug('Node: ' + result_nodes[index].toxml()) raw_name = strip_white_pattern.sub(' ', getText(name_node)) leaches = strip_white_pattern.sub(' ', getText(leaches_nodes[index])) logging.info('Raw Name: %s, Leaches: %s', raw_name, leaches) source_results.append( models.MovieListEntry(raw_movie_name=raw_name, leaches=int(leaches), active=False)) db.put(source_results) #Refresh done using map/reduce. First we map to find the movie details for source_result in source_results: taskqueue.add(url=reverse('topmovies.task_handler.find_movie'), params={'source_entry_key': source_result.key()}) return HttpResponse("Loaded results for source: %s" % str(source))
def get_free_lst(cookies): import xpath import xml.dom.minidom global round_cnt global request_cnt #request_cnt = 0 request_cnt += 1 print 'Request %d of round %d begin.'%(request_cnt, round_cnt) res = requests.get(free_url, cookies = cookies) html = res.text tbody = html[html.index('<tbody'):html.index('</tbody>') + len('</tbody>')] doc = xml.dom.minidom.parseString(tbody) entries = xpath.find('tr', doc.documentElement) #print len(entries) frees = [] i = 1 for entry in entries: tds = xpath.find('td/text()', entry) #print len(tds) free = [i] i += 1 for td in tds: free.append(str(td.toxml())) frees.append(free) #print_frees(frees) return frees
def signalNodelist(self, nodelist): """Wake up clients waiting for the given nodes""" if DEBUG: print 'signalNodelist(%s)' % repr(nodelist) for location, cv in self._waiting.items(): waitnodelist = xpath.find(location, self._doc.documentElement) for wn in waitnodelist: if wn in nodelist: #print 'DBG signal for', wn cv[1] += 1 cv[0].notify_all() break tocallback = {} for location, callback in self._callbacks: waitnodelist = xpath.find(location, self._doc.documentElement) for wn in waitnodelist: if wn in nodelist: # Add to callbacks needed if callback in tocallback: tocallback[callback].append(wn) else: tocallback[callback] = [wn] for callback, waitnodes in tocallback.items(): if DEBUG: print 'signalNodelist calling %s(%s)' % (callback, waitnodes) callback(*waitnodes)
def getProfileEndpointType(self): '''Retrieve all profile nodes and get information of EDP type''' rst_data = [] for profile_node in xpath.find('//profile', self.__xmldoc): profile_id = profile_node.getAttribute('id') profile_name = profile_node.getAttribute('name') for edp_type_node in xpath.find('//profile[@id=$id][@name=$nm]/data/endpoint_type', self.__xmldoc, id=profile_id, nm=profile_name): edp_type = {} edp_type['Device_ID'] = self.device_id edp_type['Profile'] = profile_id edp_type['Endpoint_Type'] = edp_type_node.getAttribute('id') edp_type['calibration_boost'] = edp_type_node.getElementsByTagName('calibration-boost')[0].getAttribute('value') edp_type['dialog_enhancer_enable'] = edp_type_node.getElementsByTagName('dialog-enhancer-enable')[0].getAttribute('value') edp_type['dialog_enhancer_amount'] = edp_type_node.getElementsByTagName('dialog-enhancer-amount')[0].getAttribute('value') edp_type['dialog_enhancer_ducking'] = edp_type_node.getElementsByTagName('dialog-enhancer-ducking')[0].getAttribute('value') edp_type['surround_boost'] = edp_type_node.getElementsByTagName('surround-boost')[0].getAttribute('value') edp_type['volmax_boost'] = edp_type_node.getElementsByTagName('volmax-boost')[0].getAttribute('value') edp_type['volume_leveler_enable'] = edp_type_node.getElementsByTagName('volume-leveler-enable')[0].getAttribute('value') edp_type['volume_leveler_amount'] = edp_type_node.getElementsByTagName('volume-leveler-amount')[0].getAttribute('value') edp_type['intermediate_profile_partial_virtualizer_enable'] = edp_type_node.getElementsByTagName('intermediate_profile_partial_virtualizer_enable')[0].getAttribute('value') self.__convertBoolToInt(edp_type) rst_data.append(edp_type) return rst_data
def parse_wifi_conversion(self): """ Returns retrieved information from wifi conversion file. :rtype: dict :return: Dictionary containing wifi conversion's data. """ try: document = minidom.parse(self.__directory + self.__wifi_filename) dictionnary2_4ghz = {} band_node_list = xpath.find( '/wifi_conversion/band[@name="2.4GHZ"]/channel', document) for band_node in band_node_list: band_name = self.__get_attribute_value(band_node, "name") band_mid_freq = self.__get_attribute_value( band_node, "mid_range_frequency") dictionnary2_4ghz[band_name] = float(band_mid_freq) dictionnary5ghz = {} band_node_list = xpath.find( '/wifi_conversion/band[@name="5.0GHZ"]/channel', document) for band_node in band_node_list: band_name = self.__get_attribute_value(band_node, "name") band_mid_freq = self.__get_attribute_value( band_node, "mid_range_frequency") dictionnary5ghz[band_name] = float(band_mid_freq) dictionnary = {} dictionnary["2.4GHZ"] = dictionnary2_4ghz dictionnary["5.0GHZ"] = dictionnary5ghz except Exception as exc: # pylint: disable=W0703 raise exc return dictionnary
def organizations(self): url = xpath.find( '//Session/Link[@type="application/vnd.vmware.vcloud.orgList+xml"]', session.doc)[0].getAttribute('href') doc = self.get(url) return dict([(org.getAttribute('name'), Organization(self, org.getAttribute('href'))) for org in xpath.find('//OrgList/Org', doc)])
def test_self(self): doc = xml.dom.minidom.parseString(""" <doc> <para /> </doc> """).documentElement para_node = xpath.findnode('para', doc) self.failUnlessEqual(len(xpath.find('self::para', doc)), 0) self.failUnlessEqual(len(xpath.find('self::para', para_node)), 1)
def tokensNeededByElement(self, element, optional=False): """Return a list of elements describing the tokens this element needs""" nodelist = xpath.find(".//au:needCapability", element, namespaces=NAMESPACES) if optional: nodelist += xpath.find(".//au:mayNeedCapability", element, namespaces=NAMESPACES) return nodelist
def test_partition(self): """Test that the ancestor, descendant, following, preceding, and self axes partition the document. """ a = xpath.find('//*', self.doc) a.sort() b = [] node = xpath.findnode('//*[@id="2.2"]', self.doc) for axis in ('ancestor','descendant','following','preceding','self'): b.extend(xpath.find('%s::*' % axis, node)) b.sort() self.failUnlessEqual(a, b)
def parse_error(doc): errors = xpath.find('//error', doc) if len(errors) > 0: error = errors[0] desc = xpath.find('desc/text()', error) if len(desc) > 0: desc = desc[0].data hint = xpath.find('hint/text()', error) if len(hint) > 0: hint = hint[0].data return (error.getAttribute('id'), desc, hint) return None
def test_partition(self): """Test that the ancestor, descendant, following, preceding, and self axes partition the document. """ a = xpath.find('//*', self.doc) a.sort(key=id) b = [] node = xpath.findnode('//*[@id="2.2"]', self.doc) for axis in ('ancestor', 'descendant', 'following', 'preceding', 'self'): b.extend(xpath.find('%s::*' % axis, node)) b.sort(key=id) self.assertEqual(a, b)
def getElements(self, location, context=None): """Return a list of DOM nodes (elements only, for now) that match the location""" with self: if context is None: context = self._doc.documentElement nodeList = xpath.find(location, context, originalContext=[context]) return nodeList
def _get_text_children(self, xq): self.ensure_loaded() els = xpath.find(xq, self.doc) if els: return get_text(els[0].childNodes) else: return None
def _xmlparserwithspecificxpath(self, datafile, xpathe): # type: () -> object """ Custom parser for xml using specific xpath USAGE: _xmlparserwithspecificxpath(datafile,xpathe) datafile : file containing actual xml data xpath : provide the specific xpath which needs to be parsed NOTE: This method/function can be used for any test involving xml parsing using specific xpath """ self.datafile = datafile self.xpathe = xpathe loaddatafile = xmlparse(datafile) try: response = xpath.find(xpathe, loaddatafile) for xpathee in response: for xchild in xpathee.childNodes: if xchild.nodeType == xchild.TEXT_NODE: xvalue = xchild.nodeValue print(xvalue) except TypeError: raise Exception('None') except Exception as e: raise Exception("Error Occurred : %s" % e)
def getDeviceTuning(self): '''Retrieve all tuning data''' rst_data = [] for tuning_node in xpath.find('//tuning', self.__xmldoc): tuning_data = {} tuning_data['Device_ID'] = self.device_id tuning_data['name'] = tuning_node.getAttribute('name') tuning_data['profile_restrictions'] = tuning_node.hasAttribute('profile_restrictions') and tuning_node.getAttribute('profile_restrictions') or 'none' tuning_data['classification'] = tuning_node.getAttribute('classification') tuning_data['endpoint_type'] = tuning_node.getAttribute('endpoint_type') tuning_data['mono_device'] = tuning_node.getAttribute('mono_device') tuning_data['has_sub'] = tuning_node.getAttribute('has_sub') tuning_data['tuned_rate']= tuning_node.getAttribute('tuned_rate') #Reserverd field tuning_data['preGain'] = '' #Reserverd field tuning_data['postGain'] = '' #Retrieve tuning parameters under //tuning/data self.__processTuningParams(tuning_data['name'], tuning_data['profile_restrictions'], tuning_data) #Format values, convert bool value to int value self.__convertBoolToInt(tuning_data) rst_data.append(tuning_data) return rst_data
def _mergeTree(self, context, newTree, token, plugin, namespaces=NAMESPACES): assert self.writelock().locked() newChild = newTree.firstChild toAdd = [] while newChild: # Ignore non-element children if newChild.nodeType != newChild.ELEMENT_NODE: newChild = newChild.nextSibling continue # Check whether the old tree has an element corresponding to this one xp = self._constructXPathForNewChild(newChild, plugin) matches = xpath.find(xp, context, namespaces=namespaces) if not matches: # No child exists that matches this child toAdd.append(newChild) elif len(matches) == 1: # A single child in the old tree matches. Recursively descend. newContext = matches[0] self._mergeTree(newContext, newChild, token, plugin, namespaces=namespaces) else: raise DBParamError('mergeElement: multiple matches for %s at %s' % (xp, self._getXPathForElement(context))) newChild = newChild.nextSibling for newChild in toAdd: newTree.removeChild(newChild) context.appendChild(newChild) self.mergeNodesToSignal.append(newChild)
def vapps(self): self.ensure_loaded() return dict([(e.getAttribute("name"), VApp(self.session, e.getAttribute("href"))) for e in xpath.find( "//Vdc/ResourceEntities/ResourceEntity[@type='%s']" % VApp.content_type, self.doc)])
def catalogs(self): self.ensure_loaded() els = xpath.find('//Org/Link[@type="%s"]' % Catalog.content_type, self.doc) return dict([(el.getAttribute('name'), Catalog(self.session, el.getAttribute('href'))) for el in els])
def _get_attribute(self, xq, attr, default=None): self.ensure_loaded() els = xpath.find(xq, self.doc) if els: return els[0].getAttribute(attr) else: return default
def getGEQBands(self): '''Get all IEQ bands information''' rst_data = [] for geqInst in xpath.find('//preset[@type="geq"]/@id', self.__xmldoc): rst_data.append(self.__getGEQRecord(geqInst.value)) #return geq band info return rst_data
def _ensure_items_parsed(self): self.ensure_loaded() if self._items is None: self._items = dict([(item.getAttribute('name'), item.getAttribute('href')) for item in xpath.find('//Catalog/CatalogItems/CatalogItem', self.doc)])
def test_text_children(self): doc = xml.dom.minidom.parseString(""" <doc>This is <i>some</i> text.</doc> """).documentElement result = xpath.find('text()', doc) self.assertEqual([x.data for x in result], ["This is ", " text."])
def getValues(self, location, context=None): """Return a list of node values from the document (as names and strings)""" with self: if context is None: context = self._doc.documentElement nodelist = xpath.find(location, context, originalContext=[context]) return self._getValueList(nodelist)
def test_named_attribute(self): doc = xml.dom.minidom.parseString(""" <doc name="foo" value="bar" /> """).documentElement result = xpath.find('@name', doc) self.assertEqual([(x.name, x.value) for x in result], [('name', 'foo')])
def test_text_children(self): doc = xml.dom.minidom.parseString(""" <doc>This is <i>some</i> text.</doc> """).documentElement result = xpath.find('child::text()', doc) self.failUnlessEqual([x.data for x in result], ["This is ", " text."])
def addElement(self, parentPath, tag, element, token, context=None, namespaces=NAMESPACES): """Add a new element to the database.""" # # Find parent # # NOTE: we pass the tagname for the child element. This is so put rights on a # child that does not exist yet can be checked. callbacks = None with self.writelock(): self.currentGeneration += 1 if context is None: context = self._doc.documentElement parentElements = xpath.find(parentPath, context, namespaces=namespaces) if not parentElements: raise DBParamError("Parent not found: %s" % parentPath) if len(parentElements) > 1: raise DBParamError("Multiple parents: %s" % parentPath) parent = parentElements[0] self._checkAccess('post', parent, token, tag) # # Add new node to the end of the parent # parent.appendChild(element) # # Signal both parent and new node # nodesToSignal = recursiveNodeSet(element) nodesToSignal += nodeSet(parent) callbacks = self._signalNodelist(nodesToSignal) if callbacks: self._runSignalCallbacks(callbacks)
def _xmlparser(self, datafile, xpathfile): # type: () -> object """ Custom parser for xml using xpath USAGE: _xmlparser(datafile,xpathfile) datafile : file containing actual xml data xpathfile : file containing all the xpath's mentioned in a file NOTE: This method/function can be used for any test involving xml parsing using xpath's """ self.datafile = datafile self.xpathfile = xpathfile loaddatafile = xmlparse(datafile) xpaths = open(xpathfile, 'r+') for xpathlist in xpaths: try: response = xpath.find(xpathlist, loaddatafile) for xpathe in response: for xchild in xpathe.childNodes: if xchild.nodeType == xchild.TEXT_NODE: xvalue = xchild.nodeValue print('%s : %s' % (xpathe, xvalue)) # print('%s' % xvalue) except TypeError: raise Exception('TypeError') except Exception as e: raise Exception("Error Occurred : %s" % e)
def test_all_attributes(self): doc = xml.dom.minidom.parseString(""" <doc name="foo" value="bar" /> """).documentElement result = xpath.find('attribute::*', doc) self.failUnlessEqual([(x.name, x.value) for x in result], [('name', 'foo'), ('value', 'bar')])
def hasValues(self, location, context=None): """Return a list of xpaths for the given location""" with self: if context is None: context = self._doc.documentElement nodelist = xpath.find(location, context, originalContext=[context]) return map(self.getXPathForElement, nodelist)
def refresh_movie_source(request): if 'source_key' not in request.REQUEST: return HttpResponseServerError('No source key specified in request params') source = models.MovieListingSource.get(request.REQUEST['source_key']) if not source: logging.error('Unable to find MovieListingSource: %s', request.REQUEST['source_key']) return HttpResponse('Error unable to find Source') elif not source.yql: logging.error('No yql for MovieListingSource: %s' % str(source)) return HttpResponse('No YQL for source') elif not source.settings: logging.error('No settings for MovieListingSource: %s' % str(source)) return HttpResponse('No settings for source') logging.info('Refreshing movie from source %s', str(source)) yql = source.yql if 'offset' in request.REQUEST: query_offset = int(request.REQUEST['offset']) + 1 yql = '%s limit %d offset %d' % (yql, settings.MOVIE_REFRESH_QUERY_SIZE, query_offset) form_data = urllib.urlencode({"q": yql, "format": "xml", "diagnostics": "false"}) result = urlfetch.fetch(url=settings.YQL_BASE_URL,payload=form_data,method=urlfetch.POST) dom = minidom.parseString(result.content) result_nodes = dom.getElementsByTagName('results')[0].childNodes name_nodes = xpath.find(source.settings.name_xpath, dom) leaches_nodes = xpath.find(source.settings.leaches_xpath, dom) logging.info('Found %d raw names', len(name_nodes)) strip_white_pattern = re.compile(r"\s+") source_results = [] for index, name_node in enumerate(name_nodes): logging.debug('Node: ' + result_nodes[index].toxml()) raw_name = strip_white_pattern.sub(' ', getText(name_node)) leaches = strip_white_pattern.sub(' ', getText(leaches_nodes[index])) logging.info('Raw Name: %s, Leaches: %s', raw_name, leaches) source_results.append(models.MovieListEntry(raw_movie_name=raw_name, leaches=int(leaches), active=False)) db.put(source_results) #Refresh done using map/reduce. First we map to find the movie details for source_result in source_results: taskqueue.add(url=reverse('topmovies.task_handler.find_movie'), params={'source_entry_key': source_result.key()}) return HttpResponse("Loaded results for source: %s" % str(source))
def test_root(self): doc = xml.dom.minidom.parseString(""" <doc><a><b><context /></b></a></doc> """).documentElement node = xpath.findnode('//context', doc) result = xpath.find('/', node) self.failUnlessEqual([x.nodeType for x in result], [xml.dom.Node.DOCUMENT_NODE])
def test_last_child(self): doc = xml.dom.minidom.parseString(""" <doc> <para id="1" /><para id="2" /><para id="3" /> </doc> """).documentElement result = xpath.find('para[last()]', doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["3"])
def CheckAnalysisFeature(self, xmlDoc): viewxpathstring = '/ns3:views/view[@category="' \ + self._section['apptarget'] \ + '" and @id="' \ + self._section['view'] \ + '"]' Pods = self._section['pod'].split(',') ViewNodes = xpath.find(viewxpathstring, xmlDoc) returnValue = False for nodeView in ViewNodes: returnValue = True for pod in Pods: podxpathstring = '//pod[@id="' + pod + '"]' PodNodes = xpath.find(podxpathstring, xmlDoc) if len(PodNodes) == 0: returnValue = False return(returnValue)
def test_pi_element(self): doc = xml.dom.minidom.parseString(""" <doc> <processing-instruction id="1">text</processing-instruction> </doc> """) result = xpath.find("//processing-instruction", doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["1"])
def test_comment_element(self): doc = xml.dom.minidom.parseString(""" <doc> <comment id="1">text</comment> </doc> """) result = xpath.find("//comment", doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["1"])
def getIEQBands(self): '''Get all IEQ bands information.''' rst_data = [] for ieqInst in xpath.find('//preset[@type="ieq"]/@id', self.__xmldoc): #Pass ieq ID & append the result to the list rst_data.append(self.__getIEQRecord(ieqInst.value)) #return ieq band info return rst_data
def pullValues(self, location): """Wait for values, remove them from the document, return it (as list of strings)""" with self: nodelist = xpath.find(location, self._doc.documentElement) if not nodelist: self.waitLocation(location) nodelist = xpath.find(location, self._doc.documentElement) assert nodelist rv = self._getValueList(nodelist) parentList = [] for node in nodelist: parentNode = node.parentNode parentNode.removeChild(node) if not parentNode in parentList: parentList += nodeSet(parentNode) self.signalNodelist(parentList) return rv
def plausable_codecover_field(project, version, t): with get_file(['codecover', project, version, t]) as tar: f = tar.extractfile('coverage/report_html/report_single.html') tree = parse(f) nodes = xpath.find('//span[@class="covered fullyCovered Statement_Coverage"]', tree) covered_line_numbers = [int(xpath.find("td/a/text()", node.parentNode.parentNode)[0].nodeValue) for node in nodes] covered_code = [xpath.find('text()', node)[0].nodeValue for noe in nodes] def is_ok(code): return re.match(r'(public|private|)? static (final)? (long) .* = .*;', code) is not None result = all(map(is_ok, covered_code)) print result, covered_line_numbers, covered_code return result return False
def test_self(self): doc = xml.dom.minidom.parseString(""" <doc id="0"> <para id="1"/> </doc> """).documentElement result = xpath.find('.', doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["0"])
def replace(from_doc, from_path, to_doc, to_path): '''replace one node in the destination doc with one or more nodes from the source doc''' to_node = xpath.findnode(to_path, to_doc) if to_node: parent = to_node.parentNode from_nodes = xpath.find(from_path, from_doc) for x in from_nodes: parent.insertBefore(x, to_node) parent.removeChild(to_node)
def test_node_children(self): doc = xml.dom.minidom.parseString(""" <doc>This is <i>some</i> text.</doc> """).documentElement result = xpath.find('child::node()', doc) self.failUnlessEqual([x.nodeType for x in result], [xml.dom.Node.TEXT_NODE, xml.dom.Node.ELEMENT_NODE, xml.dom.Node.TEXT_NODE])
def n2c_adapter(src_params): xmlO = xml.dom.minidom.parse(StringIO.StringIO(src_params)) doc = xmlO.documentElement ctlatlngs = map(lambda e:e.toxml(), xpath.find('/geonames/geoname/(name|lat|lng)/text()',doc)) cts = ctlatlngs[0::3] cts = map(str, cts) latlngs = zip(ctlatlngs[1::3],ctlatlngs[2::3]) return cts, latlngs
def append(from_doc, from_path, to_doc, to_path): '''append one or more nodes from the source doc to a node in the destination doc''' to_node = xpath.findnode(to_path, to_doc) if to_node: from_nodes = xpath.find(from_path, from_doc) for x in from_nodes: if x.nodeType == Node.ATTRIBUTE_NODE: to_node.setAttribute(x.name, x.value) else: to_node.appendChild(x)
def test_grandchildren(self): doc = xml.dom.minidom.parseString(""" <doc> <chapter><para id="1" /><para id="2" /></chapter> <section><para id="3" /><sub><para id="4" /></sub></section> <para id="4" /> </doc> """).documentElement result = xpath.find('child::*/child::para', doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["1", "2", "3"])
def test_root_descendant(self): doc = xml.dom.minidom.parseString(""" <doc> <para id="1"><context /></para> <para id="2" /> </doc> """).documentElement node = xpath.findnode('//context', doc) result = xpath.find('/descendant::para', node) self.failUnlessEqual([x.getAttribute("id") for x in result], ["1", "2"])
def test_titles(self): doc = xml.dom.minidom.parseString(""" <doc> <chapter id="1" /> <chapter id="2"><title /></chapter> <chapter id="3"><title /><title /></chapter> </doc> """).documentElement result = xpath.find("child::chapter[child::title]", doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["2", "3"])
def test_all_children(self): doc = xml.dom.minidom.parseString(""" <doc> <para id="1" /> <div id="2" /> <para id="3" /> </doc> """).documentElement result = xpath.find('child::*', doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["1", "2", "3"])
def test_all_but_first(self): doc = xml.dom.minidom.parseString(""" <doc> <div id="1" /><para id="2" /> <div id="3" /><para id="4" /> <div id="5" /><para id="6" /> </doc> """).documentElement result = xpath.find('child::para[position()>1]', doc) self.failUnlessEqual([x.getAttribute("id") for x in result], ["4", "6"])