SimpleTAL API: simpleElementTree Module.

Documentation on how to use the simpleElementTree Module.

simpleElementTree

This module provides integration with ElementTree, allowing XML documents to be parsed and placed directly into the Context.  The contents of the XML document can then be accessed using an sub-set of XPATH from within TAL templates.

parseFile (file)

This function takes one parameter, which should be either a file-like object or a string holding the name of a file on the filesystem.  The file is read, the XML it contains is parsed, and an instance of simpleTALES.ContextVariable is returned.

The returned ContextVariable can then be added to the Context using addGlobal in the normal way, e.g.

xmlTree = simpleElementTree.parseFile (file="input.xml")
context = simpleTALES.Context(allowPythonPath=1)
context.addGlobal ("input", xmlTree)

ElementTree Paths

The Context variable provided by parseFile provides the following syntax for path access in TAL:

  • Accessing the variable directly returns the Text value for this node.
  • Accessing variable/find/path returns the first node to match the path, as supported by ElementTree (see XPath Support in ElementTree).
  • Accessing variable/findall/path returns a list of nodes that match the path, as supported by ElementTree (see XPath Support in ElementTree).
  • Accessing variable@name returns the XML attribute "name".
  • Accessing variable/anotherElement is a short-cut for variable/find/anotherElement.

Here are some examples:

<html>
  <body>
    <h1 tal:attributes="class input/title/@type" tal:content="input/title">Title here</h1>
    <ul>
      <li tal:repeat="note input/findall/.//note">
      <b tal:condition="note/@type" tal:replace="string: Note (${note/@type}) - $note and then some"></b>
      </li>
    </ul>
  </body>
</html>
Last Modified: Sun, 01 Feb 2015 10:59:33 CET

Made with PubTal 3.5

Copyright 2021 Colin Stewart

Email: colin at owlfish.com