#!/usr/bin/env python from lxml import etree import simplejson, sys, os, cgi, cgitb; cgitb.enable() nidStr = "{http://www.hyperscope.org/hyperscope/opml/public/2006/05/09}nid" augEtree = etree.parse(open("augmentinghumanintellect.opml")) query = cgi.FieldStorage().getfirst("q", "") if len(query): # FIXME: attribute searches seem broken in lxml # nodes = augEtree.xpath("//outline[@hs:nid='"+query+"']") nodes = [ child for child in augEtree.xpath("//outline") if child.attrib[nidStr] == query ] else: nodes = augEtree.xpath("//body/outline") result = [] for node in nodes: result.append({ "text": node.attrib["text"], "id": node.attrib[nidStr], "children": [ child.attrib[nidStr] for child in node ] }) print """Content-Type: text/html\n\n""" print simplejson.dumps(result) # vim:ft=python:ts=4:noet: