To help deploy XML files, I use the Python SAX handler, as shown below. Can someone provide equivalent XSLT to do the same job? This is an example input file:
<beatles> <beatle> <name> <first>John</first> <last>Lennon</last> </name> </beatle> <beatle> <name> <first>Paul</first> <last>McCartney</last> </name> </beatle> <beatle> <name> <first>George</first> <last>Harrison</last> </name> </beatle> <beatle> <name> <first>Ringo</first> <last>Starr</last> </name> </beatle> </beatles>
So, the idea is to get a list of all unique paths (ignoring attributes), to get a basic starting point for writing patterns, etc.
from xml.sax.handler import ContentHandler from xml.sax import make_parser from xml.sax import SAXParseException class ShowPaths(ContentHandler): def startDocument(self): self.unique_paths=[] self.current_path=[] def startElement(self,name,attrs): self.current_path.append(name) path="/".join(self.current_path) if path not in self.unique_paths: self.unique_paths.append(path) def endElement(self,name): self.current_path.pop(); def endDocument(self): for path in self.unique_paths: print path if __name__=='__main__': handler = ShowPaths() saxparser = make_parser() saxparser.setContentHandler(handler) in_f=open("d:\\beatles.xml","r") saxparser.parse(in_f) in_f.close()
And the result of starting the program as an example:
beatles beatles/beatle beatles/beatle/name beatles/beatle/name/first beatles/beatle/name/last
source share