XML Functions

Although Myokit’s mmt format is not an XML format, Myokit interacts with XML formats in various ways. The myokit.mxml module provides XML tools for common tasks. This module is imported as part of the main myokit package.

Dom traversal

myokit.mxml.dom_child(node, selector=None)

Returns the first child element of the given DOM node.

If the optional selector is given it searches for an element of a particular type.

Returns None if no such node is found.

myokit.mxml.dom_next(node, selector=False)

Returns the next sibling element after the given DOM node.

If the optional selector is given it searches for an element of a particular type.

Returns None if no such node is found.

Converting html to ascii

myokit.mxml.html2ascii(html, width=79, indent=' ')

Flattens HTML and attempts to create readable ASCII code.

The ascii will be text-wrapped after width characters. Each new level of nesting will be indented with the text given as indent.

Writing html documents

class myokit.mxml.TinyHtmlPage

Can be used to create tiny html pages, in an object oriented way.

Every page has a head and a body. The head contains meta information and can be manipulated using the methods of the TinyHtmlPage object. The body is represented as a TinyHtmlNode and can contain nodes (corresponding to html tags) out of a small subset of HTML.

** Example **

The example below shows how you can create a very simple html page.

>>> import myokit
... page = myokit.mxml.TinyHtmlPage()
... page.set_title('TinyHtml test page')
... p = page.append('p')
... p.text('This is some text. ')
... e = p.append('em')
... e.text('This bit is emphasised! ')
... p.text('This bit comes after the emphasised bit.')
... p.append('strong').text('You can use chaining too!')

Now calling “print(page)” should output a complete web page.

append(name)

Appends an element to this page and returns the created node.

html(pretty=False)

Returns this page’s html

set_title(title)

Changes this page’s title

class myokit.mxml.TinyHtmlNode(name, definitions=None)

TinyHtml pages are built up out of TinyHtmlNode objects. TinyHtmlNodes are constructed following the scheme laid out in the TinyHtmlScheme object.

append(name)

Creates a nodeppends it to this nodend returns the new node.

attr(attribute)

Returns the value set for this attribute

html()

Returns this node’s html representation

math(expr)

Adds a mathml element to this node. The element’s contents will be set using the write_mathml() method on the given myokit Equation or Expression.

set_attr(attribute, value)

Sets the value of attribute attribute to value. Returns this node for chaining.

text(text)

Creates and appends a text node to this node.

class myokit.mxml.TinyHtmlScheme

Acts as a namespace for the static lists and defintions used by TinyHtmlNodes.

A node definition follows the structure of a Document Type Definition (DTD). This DTD defines which elements exist, which attributes they may have and which child elements they can contain. A special element “PCDATA” is used to indicate the textual contents of a node (if any).

Grouping

The elements (which will be defined in detail later) are grouped together in the following categories:

formatting
em, strong, q
special
br, img, code
heading
h1, h2, h3
block
div, heading, p, table, ol, ul, dl
inline
pcdata, formatting, special, a
inblock
pcdata, formatting, special, a, block

Formatting tags

em
Used for emphasis, this usually means “italic” Attributes: id, class, style Children: inline
strong
Used for “strong” aka bolded text Attributes: id, class, style Children: inline
q
Used for (inline) quotes Attributes: id, class, style Children: inline

** Special tags **

br
This is a line break Attributes: None Children: None
img
An image Attributes: id, class, alt, src (required) Children: None
code
Represents a block of code Attributes: id, class, style Children: PCDATA

** Headings **

h1
The first heading Attributes: id, class, style Children: PCDATA
h2
The second heading Attributes: id, class, style Children: PCDATA
h3
The third heading (three is enough) Attributes: id, class, style Children: PCDATA

** Divisions **

div
A division Attributes: id, class, style Children: inblock

** Paragraph **

p
A paragraph Attributes: id, class, style Children: inline

** Lists **

ul
An unordered (bulleted) list Attributes: id, class, style Children: li (one or more)
ol
An ordered (numbered) list Attributes: id, class, style Children: li (one or more)
li
A list item Attributes: id, class, style Children: inblock
dl
A definition list Attributes: id, class, style Children: One or more pairs (dt, dd)
dt
A term to be defined Attributes: id, class, style Children: inline
dd
A term’s definition Attributes: id, class, style Children: inblock

** Tables **

table
A table Attributes: id, class, style Children: tr
thead
A table header Attributes: id, class, style Children: tr
tbody
A table footer Attributes: id, class, style Children: tr
tbody
A table body Attributes: id, class, style Children: tr
tr
A row in a table Attributes: id, class, style Children: th, td
th
A cell in a table, formatted as a special header Attributes: id, class, style, rowspan, colspan Children: inblock
td
An ordinary cell in a table Attributes: id, class, style, rowspan, colspan Children: inblock

** The anchor element **

a
An anchor (a hyperlink) Attributes: id, class, style, href (required) Children: PCDATA, formatting, special

** The document body **

body
The document body Attributes: none Children: inblock

** Math **

math
A mathml container Attributes: id, class, xmlns (required, but set automatically) Children: Set automatically
myokit.mxml.write_mathml(expression, presentation)

Converts a myokit Expression to a mathml expression.

The boolean argument presentation can be used to select between Presentation MathML and Content MathML.