- parser = etree.XMLParser(remove_blank_text=True)
- return cls( etree.parse(StringIO(data), parser) )
- except XMLSyntaxError, e:
- raise ParseError(e.message)
- except ExpatError, e:
- raise ParseError(e.message)
+ parser = etree.XMLParser(remove_blank_text=False)
+ return cls(etree.parse(StringIO(data), parser), parse_dublincore=parse_dublincore)
+ except (ExpatError, XMLSyntaxError, XSLTApplyError), e:
+ raise ParseError(e)
+
+ def chunk(self, path):
+ # convert the path to XPath
+ expr = self.path_to_xpath(path)
+ elems = self.edoc.xpath(expr)
+
+ if len(elems) == 0:
+ return None
+ else:
+ return elems[0]
+
+ def path_to_xpath(self, path):
+ parts = []
+
+ for part in path.split('/'):
+ match = re.match(r'([^\[]+)\[(\d+)\]', part)
+ if not match:
+ parts.append(part)
+ else:
+ tag, n = match.groups()
+ parts.append("*[%d][name() = '%s']" % (int(n)+1, tag) )
+
+ if parts[0] == '.':
+ parts[0] = ''
+
+ return '/'.join(parts)