def add_virtual_elements(self, xml, filename): """Adds in virtual elements into the XML tree to represent information about the file or formatting.""" # Go through all the author tags and add a formatted version. for info in xml.xpath("//d:personname", namespaces=xml_ns): # Pull out the components of the name. firstname = _get_element_value(info, "firstname", None) surname = _get_element_value(info, "surname", None) name = surname if firstname: name += ", " + firstname # Pull the name back in. fullname = lxml.etree.Element(docbook_lxml_ns + "fullname") fullname.text = name info.append(fullname) # Go through all the info tags and add a formatted version of # the titles. for info in xml.xpath("//d:info", namespaces=xml_ns): # Pull out the components of the name. title = _get_element_value(info, "title", None) subtitle = _get_element_value(info, "subtitle", None) combined = title if subtitle: combined += ": " + subtitle # Pull the name back in. fulltitle = lxml.etree.Element(docbook_lxml_ns + "fulltitle") fulltitle.text = combined info.append(fulltitle) # Put in the absolute filename. abspath = lxml.etree.Element(docbook_lxml_ns + "abspath") abspath.text = os.path.abspath(filename) info.append(abspath)
def select_file(self, xml): """Retrieves the fields from the given XML file and writes it out to the stream.""" # Go through the select expressions and process each one. fields = [] # Get the root for the select query and keep it since we'll # loop through these quite a few times (once per field). select_roots = xml.xpath(self.args.select_root, namespaces=xml_ns) # Go through each of the select queries first. We treat these # as a single field which we'll combine together. for select_xpath in self.args.select: # Create a list of results we find from this field, which # we'll combine together into a single "field". values = [] # Loop through all the resulting root nodes so we can do a # xpath against the root. for select_root in select_roots: # Perform the query on the select root for the path. selects = select_root.xpath(select_xpath, namespaces=xml_ns) for select in selects: if select is None: continue if isinstance(select, basestring): values.append(select) else: if select.text is not None: values.append(select.text) # Once we finish gathering up all the values, we combine # them together and add it to the resulting output fields. value_string = ", ".join(values) fields.append(value_string) # Output the resulting fields to the output stream. print "\t".join(fields)