def Xpath(xpath, node): if type(xpath) in (str, unicode): xpath = xml.xpath.Compile(xpath) c = xml.xpath.Context.Context(node) nodes = xpath.evaluate(c) return nodes
def XPath(self, xpath, node=None): if not node: node = self.result if type(xpath) in (str, unicode): xpath = xml.xpath.Compile(xpath) c = xml.xpath.Context.Context(node) c.setNamespaces(namespaces) nodes = xpath.evaluate(c) return nodes
def ExtractArgument(self, name, xpath): '''Extracts name, value pairs from an XML fragment using xpath. Values are quoted for use in a url args is a dictionary mapping names to xpath expressions xmlnode is DOM node for constructing a context appends results to env and errs, returning true if no errors were generated ''' if type(xpath) == str: xpath = xml.xpath.Compile(xpath) nodes = xpath.evaluate(self.context) # expects a single result node if len(nodes) == 1: node = nodes[0] if node.nodeName == "#text": # text node value = node.nodeValue elif node.nodeValue == None: # element node value = node.localName else: # attribute node, or something we don't want value = node.nodeValue self[name] = Quote(value.strip()) return True elif len(nodes) == 2: node = nodes[0] if node.nodeName == "#text": # text node value = node.nodeValue + "," + nodes[1].nodeValue elif node.nodeValue == None: # element node value = node.localName else: # attribute node, or something we don't want value = node.nodeValue self[name] = Quote(value.strip()) return True else: errtext = "Found %s nodes matching xpath expression; expected 1 (%s)" err = ValueError(errtext % (len(nodes), xpath)) self.errs[name] = err return False