Link: start Link: parent 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) XIST examples ============= An introduction to XIST by examples =================================== Home > Python software > ll.xist > Examples 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 Creating HTML ============= You can create and output HTML like this: from ll.xist import xsc from ll.xist.ns import html, xml, meta node = xsc.Frag( · xml.XML(), · html.DocTypeXHTML10transitional(), · html.html( · · html.head( · · · meta.contenttype(), · · · html.title("Example page") · · ), · · html.body( · · · html.h1("Welcome to the example page"), · · · html.p( · · · · "This example page has a link to the ", · · · · html.a("Python home page", href="http://www.python.org/"), · · · · "." · · · ) · · ) · ) ) print node.conv().bytes(encoding="us-ascii") You can also use with blocks (and the unary + operator) to generate the same HTML: from ll.xist import xsc from ll.xist.ns import html, xml, meta with xsc.build(): · with xsc.Frag() as node: · · +xml.XML() · · +html.DocTypeXHTML10transitional() · · with html.html(): · · · with html.head(): · · · · +meta.contenttype() · · · · +html.title("Example page") · · · with html.body(): · · · · +html.h1("Welcome to the example page") · · · · with html.p(): · · · · · +xsc.Text("This example page has a link to the ") · · · · · with html.a(): · · · · · · with xsc.addattr("href"): · · · · · · · +xsc.Text(""http://www.python.org/"") · · · · · · +xsc.Text("Python home page") · · · · · +xsc.Text(".") print node.conv().bytes(encoding="us-ascii") Defining new elements ===================== You can define new elements and how they should be converted to HTML (or other XML vocabularies) like this: from ll.xist import xsc from ll.xist.ns import html, xml, meta class cheeseshoplink(xsc.Element): · class Attrs(xsc.Element.Attrs): · · class name(xsc.TextAttr): pass · def convert(self, converter): · · e = html.a( · · · self.attrs.name, · · · href=("http://cheeseshop.python.org/pypi/", self.attrs.name) · · ) · · return e.convert(converter) names = ["ll-xist", "cx_Oracle", "PIL"] node = xsc.Frag( · xml.XML(), · html.DocTypeXHTML10transitional(), · html.html( · · html.head( · · · meta.contenttype(), · · · html.title("Cheeseshop links") · · ), · · html.body( · · · html.h1("Cheeseshop links"), · · · html.ul(html.li(cheeseshoplink(name=name)) for name in names) · · ) · ) ) print node.conv().bytes(encoding="us-ascii") Parsing HTML ============ Parsing HTML is done like this: from ll.xist import parse from ll.xist.ns import html node = parse.tree( · parse.URL("http://www.python.org/"), · parse.Tidy(), · parse.NS(html), · parse.Node() ) Finding and counting nodes ========================== The following example shows you how to output the URLs of all images inside links on Python's homepage: >>> from ll.xist import parse >>> from ll.xist.ns import html >>> node = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Expat(ns=True), ... parse.Node() ... ) >>> for img in node.walknodes(html.a/html.img): ... print img.attrs.src ... http://www.python.org/images/python-logo.gif http://www.python.org/images/trans.gif http://www.python.org/images/trans.gif http://www.python.org/images/success/nasa.jpg If you want to output both the links and the image URLs, do the following: >>> from ll.xist import parse, xfind >>> from ll.xist.ns import html >>> node = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Expat(ns=True), ... parse.Node() ... ) >>> for path in node.walkpaths(html.a/html.img): ... print path[-2].attrs.href, path[-1].attrs.src http://www.python.org/ http://www.python.org/images/python-logo.gif http://www.python.org/#left%2dhand%2dnavigation http://www.python.org/images/trans.gif http://www.python.org/#content%2dbody http://www.python.org/images/trans.gif http://www.python.org/about/success/usa http://www.python.org/images/success/nasa.jpg If you want to count the number of links on the page you can do the following: >>> from ll import misc >>> from ll.xist import parse >>> from ll.xist.ns import html >>> node = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Expat(ns=True), ... parse.Node() ... ) >>> misc.count(node.walk(html.a)) 83 Replacing text ============== This example demonstrates how to make a copy of an XML tree with some text replacements: from ll.xist import xsc, parse def p2p(node, converter): · if isinstance(node, xsc.Text): · · node = node.replace("Python", "Parrot") · · node = node.replace("python", "parrot") · return node node = parse.tree( · parse.URL("http://www.python.org/"), · parse.Expat(ns=True), · parse.Node() ) node = node.mapped(p2p) node.write(open("parrot_index.html", "wb")) Converting HTML to XIST code ============================ The class ll.xist.present.CodePresenter makes it possible to output an XIST tree as usable Python source code: >>> from ll.xist import parse, present >>> node = parse.tree( ... parse.URL("http://www.python.org/"), ... parse.Expat(ns=True), ... parse.Node() ... ) >>> print present.CodePresenter(node) ll.xist.xsc.Frag( · ll.xist.ns.html.html( · · ll.xist.ns.html.head( · · · ll.xist.ns.html.meta( · · · · http_equiv='content-type', · · · · content='text/html; charset=utf-8' · · · ), · · · ll.xist.ns.html.title( · · · · 'Python Programming Language -- Official Website' · · · ), · · · ll.xist.ns.html.meta( · · · · name='keywords', · · · · content='python programming language object oriented web free source' · · · ), · · · [... Many lines deleted ...] · · · · · · u'\n\tCopyright \xa9 1990-2007, ', · · · · · · ll.xist.ns.html.a( · · · · · · · 'Python Software Foundation', · · · · · · · href='http://www.python.org/psf' · · · · · · ), · · · · · · ll.xist.ns.html.br(), · · · · · · ll.xist.ns.html.a( · · · · · · · 'Legal Statements', · · · · · · · href='http://www.python.org/about/legal' · · · · · · ), · · · · · · '\n ', · · · · · · id='footer' · · · · · ), · · · · · '\n\n\n ', · · · · · id='body-main' · · · · ), · · · · '\n ', · · · · id='content-body' · · · ), · · · '\n' · · ), · · lang='en' · ) ) Using converter contexts to pass information between elements ============================================================= Converter contexts can be used to pass information between elements. The following example will generate HTML