Link: start Link: parent Link: First page in set (first) Link: Previous page (previous) Link: Next page (next) Link: Last page in set (last) Link: A plain text version of this page (alternate) Link: The XIST source of this page (alternate) Transforming XIST trees ======================= The mapped function, sorting, etc. ================================== Home > Python software > ll.xist > Transformation Text · XIST Python softwarelist of projects * ll.xistAn extensible XML/HTML generator * ExamplesParsing/creating/modifying XML; Traversing XML trees * HowtoExplains parsing/generating XML files, XML transformations via XIST classes and other basic concepts. * SearchingHow to iterate through XIST trees * TransformationHow to transform XIST trees * Advanced topicsPool chaining, converter contexts, validation * MiscellaneousExplains various odds and ends of XIST * xscXIST core classes * nsPackage containing namespace modules * parseParsing XML * presentScreen output of XML trees * simsSimple schema validation * xfindTree iteration and filtering * cssCSS related functions * scriptsScripts for text conversion and creating XIST namespaces * HistoryChangeLog for XIST * InstallationHow to install and configure XIST * MigrationHow to update your code to new versions of XIST * Mailing listsHow to subscribe to the XIST mailing lists * ll.ul4cA templating language * ll.urlRFC 2396 compliant URLs * ll.makeObject oriented make replacement * ll.daemonForking daemon processes * ll.sisyphusWriting cron jobs with Python * ll.colorRGB color values and color model conversion * ll.miscMisc utility functions and classes * ll.orasqlUtilities for cx_Oracle * ll.nightshadeServe the output of Oracle functions/procedures with CherryPy * ll.scriptsScripts for UL4 template rendering and URL handling * AploraLogging Apache HTTP requests to an Oracle database * PycocoPython code coverage * DownloadLinks to Windows and Linux, source and binary distributions * Source codeAccess to the Mercurial repositories Apart from the convert method, XIST provides many tools for manipulating an XML tree. The withsep method ================== The method withsep can be used to put a seperator node between the child nodes of an Element or a Frag: >>> from ll.xist import xsc >>> from ll.xist.ns import html >>> node = html.div(xrange(10)) >>> print node.withsep(", ").bytes()
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The shuffled method =================== The method shuffled returns a shuffled version of the Element or Frag: >>> from ll.xist import xsc >>> from ll.xist.ns import html >>> node = html.div(xrange(10)) >>> print node.shuffled().withsep(", ").bytes()
8, 1, 3, 6, 7, 5, 2, 9, 4, 0
The reversed and sorted methods =============================== There are methods named reversed and sorted that return a reversed or sorted version of an element or fragment: >>> from ll.xist import xsc >>> from ll.xist.ns import html >>> def key(n): ... return unicode(n) >>> node = html.div(8,4,2,1,9,6,3,0,7,5) >>> print node.sorted(key=key).reversed().withsep(",").bytes()
9,8,7,6,5,4,3,2,1,0
The mapped method ================= The method mapped recursively walks the tree and generates a new tree, where all the nodes are mapped through a function. An example: To replace Python with Parrot in every text node on the Python page, do the following: from ll.xist import xsc, parse def p2p(node, converter): · if isinstance(node, xsc.Text): · · node = node.replace(u"Python", u"Parrot") · · node = node.replace(u"python", u"parrot") · return node node = parse.tree(parse.URL("http://www.python.org"), parse.Tidy(), parse.NS(html), parse.Node(pool=xsc.Pool(xml, html))) node = node.mapped(p2p) node.write(open("parrot_index.html", "wb")) The function must either return a new node, in which case this new node will be used instead of the old one, or return the old node to tell mapped that it should recursively continue with the content of the node.