Kotoba XML Parser with Level-3 CSS Selector

Version:2.1
Compatible:Yotsuba 3.0, Yotsuba 3.1, Yotsuba 4

Kotoba is an XML parsing module mainly using CSS selectors level 3. This module is inspired by the well-known jQuery JavaScript library. However, unlink jQuery, Kotoba strictedly follows the working draft of CSS selectors level 3 (W3C proposed recomendation).

class yotsuba.lib.kotoba.DOMElement(name, **kwargs)

DOM element of the document.

name is the name of the node.

append(node)

Append a node of DOMElement or DataElement as the last child of this node.

children(query=None)

Get the children of this node.

If the optional parameter query is not provided, it will return all children. Otherwise, this will works like self.get, except it only looks for the direct descendants with the query.

data(**kwargs)

Recursively get the (text) data of all nodes in list.

Returns as a string where the data from each node and its descendants is concatenated with an empty character.

get(selector, **kwargs)

Get the descendant with the selector (as string or unicode) which is a valid CSS3 selector.

Returns the result as DOMElements.

get_name()

Get the name of the element

guid()

GUID of the vertex

hash()

Hash code of the element

identifier()

Text-friendly representation of the element

init(name, adjacents=None)

Secondary constructor for lazy instantiation or resetting.

See the constructor for the usage.

static is_abstract_of(obj_ref)

Check if the object is an instance of this class.

is_connected_to(vertex)

Check if vertex (required, graph.Vertex-based object) is connected with this node.

static make(reference, **kwargs)

Instantiate from an instance of XMLNode (in xml.dom.minidom). This is a static method.

make_edge_to(vertex)

Make an edge to the another vertex (required, graph.Vertex-based object).

next(new_reference=None)

Get the next node.

If the optional parameter new_reference is provided, the current reference will be replaced.

parent(new_reference=None)

Get the parent of this node.

If the optional parameter new_reference is provided, the current reference will be replaced.

prev(new_reference=None)

Get the previous node.

If the optional parameter new_reference is provided, the current reference will be replaced.

set_name(new_name)

Set the name of the element with new_name (as string or unicode).

class yotsuba.lib.kotoba.DOMElements

Group of reference to DOM elements

append

L.append(object) – append object to end

count

L.count(value) -> integer – return number of occurrences of value

data()

Recursively get the (text) data of all nodes in list.

Returns as a string where the data from each node and its descendants is concatenated with an empty character.

extend

L.extend(iterable) – extend list by appending elements from the iterable

get(query, **kwargs)

Get the descendant with the query (as string or unicode) which is a valid CSS3 selector.

Returns the result as DOMElements.

get_edges_to_others()

Get the list of all vertices of the other graphs that connecting to this graph. (Make a connected-or-not-connected subgraph from it.)

index

L.index(value, [start, [stop]]) -> integer – return first index of value. Raises ValueError if the value is not present.

insert

L.insert(index, object) – insert object before index

static is_abstract_of(obj_ref)

Check if the object is an instance of this class.

pop

L.pop([index]) -> item – remove and return item at index (default last). Raises IndexError if list is empty or index is out of range.

remove

L.remove(value) – remove first occurrence of value. Raises ValueError if the value is not present.

reverse

L.reverse() – reverse IN PLACE

sort

L.sort(cmp=None, key=None, reverse=False) – stable sort IN PLACE; cmp(x, y) -> -1, 0, 1

class yotsuba.lib.kotoba.DataElement(data_string)

Data structure representing a data block in an XML document

guid()

GUID of the vertex

init(name, adjacents=None)

Secondary constructor for lazy instantiation or resetting.

See the constructor for the usage.

static is_abstract_of(obj_ref)

Check if the object is an instance of this class.

is_connected_to(vertex)

Check if vertex (required, graph.Vertex-based object) is connected with this node.

make_edge_to(vertex)

Make an edge to the another vertex (required, graph.Vertex-based object).

class yotsuba.lib.kotoba.Kotoba(source=None)

XML Parser with Level-3 CSS Selectors

Currently supported selectors:

  • selectors with at least one of element name, attributes and pseudo classes (only :root and :empty)
  • all four combinations of selectors are supported (e.g., selector_1 operator_1 selector_2 ...)
  • support wildcard search (only for element name)
  • support multi combinations in a single statement (e.g., combo_1, combo_2)
get(selector, **kwargs)

Get the element from the selector (as string or unicode). If the optional parameter root_node (as DOMElement) is given, Kotoba will use the given node as the starting point instead of the default one.

get_root()

Get the root element.

read(source)

Read the data (strictly well-formatted XML or Kotoba-compatible) from the source.

Source can be a file name, XML-formatted string or DOMElement (Yotsuba 3’s).

exception yotsuba.lib.kotoba.KotobaCheckpointScreeningException

Exception for failing a series of assertions when Kotoba tries to determine whether Kotoba requires to proceed the current iteration. If this error is thrown, it is definitely a bug.

exception yotsuba.lib.kotoba.KotobaGraphException

Exception when Kotaba cannot create a graph from the source. See the documentation for xml.dom.minidom (Python’s built-in module) for more information on the supported formats.

exception yotsuba.lib.kotoba.KotobaInvalidSelectionException

Exception for invalid selector. See the W3C document on level-3 CSS selector and the documentation of what kotoba.Kotoba supports.

exception yotsuba.lib.kotoba.KotobaQueryException

Exception for error during iteration. This is a critical error.

exception yotsuba.lib.kotoba.KotobaSourceException

Exception for invalid source when Kotoba reads the source. The source may be neither supported data type (string or kotoba.DOMElement) nor existed file.

See also

Module yotsuba.core.graph Graph API

Module yotsuba.core.fs
File System API

Previous topic

Graph API

Next topic

Simplified HTTP Module

This Page