gbElement class
- class xgbxml.xgbxml.gbElement[source]
A base class which is inherited by all xgbxml element instances.
When using either
get_parser()
orcreate_gbXML()
, all elements created will have the properties and methods available in this class.- __repr__()[source]
The repr for the class.
Determines how the instance is displayed when printed.
- Returns:
A different value is returned depending on if this is the gbElement class or a subclass, and/or if the element has an ‘id’ attribute.
- Return type:
str
- add_child(child_nntag, value=None, **kwargs)[source]
Adds a new child element to the element.
- Parameters:
child_nntag (str) – The ‘no namespace’ tag of the child element (i.e. “Campus”)
value (str, float, bool etc.) – The value for the element. Optional.
kwargs – Attributes to be set for the child element. Optional.
- Raises:
KeyError – If the child name does not exist in the schema.
- Raises:
Other error may be raised if the optional value or attributes are not specified correctly.
- Returns:
The newly created child element.
- Return type:
(subclass of) gbElement
- get_attribute(attribute_name)[source]
Returns the attribute value of the element.
- Parameters:
attribute_name – The name of the attribute.
attribute_name – str
- Raises:
KeyError – If the attribute is not present in the element.
- Returns:
The text value of the attribute converted to the python type of the attribute.
- Return type:
bool, str or float etc.
- property get_attributes
Returns the attributes of the element.
- Parameters:
gbxml_element (lxml.etree._Element) – A gbXML element.
xsd_schema (lxml.etree._Element) – The root node of a gbXML schema.
- Returns:
A dictionary of attributes where the attribute values have been converted to the correct python types according to the schema.
- Return type:
dict
- get_child(child_nntag, child_id=None)[source]
Returns a child element with specified tag.
If child_id is not supplied, then the first child element found is returned.
- Parameters:
child_nntag (str) – The ‘no namespace’ tag of the child element.
child_id (str) – Optional, the ‘id’ attribute of the child element.
- Raises:
KeyError – If the child element is not present.
- Return type:
(subclass of) gbElement
- get_children(child_nntag)[source]
Returns all child element with specified tag.
- Parameters:
child_nntag (str) – The ‘no namespace’ tag of the child element (i.e. “Campus”)
- Return type:
- property id
Returns / sets the ‘id’ attribute of the element.
- Parameters:
value (str) – When setting, the value of the id attribute
- Raises:
KeyError – When returning, if the ‘id’ attribute is not present in the element.
KeyError – When setting, if attribute name does not exist in the schema.
TypeError – When setting, if the attribute value is of a type that does not match the schema.
- Return type:
str (when returning)
- property nntag
Returns the tag without the namespace (‘no namespace tag’)
- Return type:
str
Code Example
>>> print(gbxml.tag) {http://www.gbxml.org/schema}gbXML >>> print(gbxml.nntag) gbXML
- property ns
The namespace dictionary for xpath calls.
- Return type:
dict
- set_attribute(attribute_name, value)[source]
Sets an attribute value of the element.
Attribute will be created if it does not already exist. Attribute value is modified if attribute does already exist.
- Parameters:
attribute_name – The name of the attribute.
attribute_name – str
value (bool, str, float) – The new value for the attribute.
- Raises:
KeyError – If attribute name does not exist in the schema.
ValueError – If attribute has enumerations, and ‘value’ does not match one of the enumeration options.
TypeError – If the attribute value is of a type that does not match the schema.
- property value
Returns / set the value of the gbXML element.
This is stored in the text value of the XML element.
- Parameters:
value (str, float, bool etc.) – When setting, the value for the element.
- Raises:
TypeError – When setting, if value is of a type that does not match the schema.
- Returns:
When returning, a value which is converted from the element text node.
- Return type:
str, int, float or book etc.