1
# public Cython/C interface to lxml.etree
3
from lxml.includes cimport tree
4
from lxml.includes.tree cimport const_xmlChar
6
cdef extern from "lxml-version.h":
7
cdef char* LXML_VERSION_STRING
9
cdef extern from "etree_defs.h":
10
# test if c_node is considered an Element (i.e. Element, Comment, etc.)
11
cdef bint _isElement(tree.xmlNode* c_node) nogil
13
# return the namespace URI of the node or NULL
14
cdef const_xmlChar* _getNs(tree.xmlNode* node) nogil
16
# pair of macros for tree traversal
17
cdef void BEGIN_FOR_EACH_ELEMENT_FROM(tree.xmlNode* tree_top,
18
tree.xmlNode* start_node,
19
int start_node_inclusive) nogil
20
cdef void END_FOR_EACH_ELEMENT_FROM(tree.xmlNode* start_node) nogil
22
cdef extern from "lxml.etree_api.h":
24
# first function to call!
25
cdef int import_lxml__etree() except -1
27
##########################################################################
28
# public ElementTree API classes
30
cdef class lxml.etree._Document [ object LxmlDocument ]:
31
cdef tree.xmlDoc* _c_doc
33
cdef class lxml.etree._Element [ object LxmlElement ]:
35
cdef tree.xmlNode* _c_node
37
cdef class lxml.etree.ElementBase(_Element) [ object LxmlElementBase ]:
40
cdef class lxml.etree._ElementTree [ object LxmlElementTree ]:
42
cdef _Element _context_node
44
cdef class lxml.etree.ElementClassLookup [ object LxmlElementClassLookup ]:
45
cdef object (*_lookup_function)(object, _Document, tree.xmlNode*)
47
cdef class lxml.etree.FallbackElementClassLookup(ElementClassLookup) \
48
[ object LxmlFallbackElementClassLookup ]:
49
cdef ElementClassLookup fallback
50
cdef object (*_fallback_function)(object, _Document, tree.xmlNode*)
52
##########################################################################
53
# creating Element objects
55
# create an Element for a C-node in the Document
56
cdef _Element elementFactory(_Document doc, tree.xmlNode* c_node)
58
# create an ElementTree for an Element
59
cdef _ElementTree elementTreeFactory(_Element context_node)
61
# create an ElementTree subclass for an Element
62
cdef _ElementTree newElementTree(_Element context_node, object subclass)
64
# create a new Element for an existing or new document (doc = None)
65
# builds Python object after setting text, tail, namespaces and attributes
66
cdef _Element makeElement(tag, _Document doc, parser,
67
text, tail, attrib, nsmap)
69
# create a new SubElement for an existing parent
70
# builds Python object after setting text, tail, namespaces and attributes
71
cdef _Element makeSubElement(_Element parent, tag, text, tail,
74
# deep copy a node to include it in the Document
75
cdef _Element deepcopyNodeToDocument(_Document doc, tree.xmlNode* c_root)
77
# set the internal lookup function for Element/Comment/PI classes
78
# use setElementClassLookupFunction(NULL, None) to reset it
79
# note that the lookup function *must always* return an _Element subclass!
80
cdef void setElementClassLookupFunction(
81
object (*function)(object, _Document, tree.xmlNode*), object state)
83
# lookup function that always returns the default Element class
84
# note that the first argument is expected to be None!
85
cdef object lookupDefaultElementClass(_1, _Document _2,
88
# lookup function for namespace/tag specific Element classes
89
# note that the first argument is expected to be None!
90
cdef object lookupNamespaceElementClass(_1, _Document _2,
93
# call the fallback lookup function of a FallbackElementClassLookup
94
cdef object callLookupFallback(FallbackElementClassLookup lookup,
95
_Document doc, tree.xmlNode* c_node)
97
##########################################################################
98
# XML attribute access
100
# return an attribute value for a C attribute on a C element node
101
cdef object attributeValue(tree.xmlNode* c_element,
102
tree.xmlAttr* c_attrib_node)
104
# return the value of the attribute with 'ns' and 'name' (or None)
105
cdef object attributeValueFromNsName(tree.xmlNode* c_element,
106
const_xmlChar* c_ns, const_xmlChar* c_name)
108
# return the value of attribute "{ns}name", or the default value
109
cdef object getAttributeValue(_Element element, key, default)
111
# return an iterator over attribute names (1), values (2) or items (3)
112
# attributes must not be removed during iteration!
113
cdef object iterattributes(_Element element, int keysvalues)
115
# return the list of all attribute names (1), values (2) or items (3)
116
cdef list collectAttributes(tree.xmlNode* c_element, int keysvalues)
118
# set an attribute value on an element
119
# on failure, sets an exception and returns -1
120
cdef int setAttributeValue(_Element element, key, value) except -1
122
# delete an attribute
123
# on failure, sets an exception and returns -1
124
cdef int delAttribute(_Element element, key) except -1
126
# delete an attribute based on name and namespace URI
127
# returns -1 if the attribute was not found (no exception)
128
cdef int delAttributeFromNsName(tree.xmlNode* c_element,
129
const_xmlChar* c_href, const_xmlChar* c_name)
131
##########################################################################
132
# XML node helper functions
134
# check if the element has at least one child
135
cdef bint hasChild(tree.xmlNode* c_node) nogil
137
# find child element number 'index' (supports negative indexes)
138
cdef tree.xmlNode* findChild(tree.xmlNode* c_node,
139
Py_ssize_t index) nogil
141
# find child element number 'index' starting at first one
142
cdef tree.xmlNode* findChildForwards(tree.xmlNode* c_node,
143
Py_ssize_t index) nogil
145
# find child element number 'index' starting at last one
146
cdef tree.xmlNode* findChildBackwards(tree.xmlNode* c_node,
147
Py_ssize_t index) nogil
149
# return next/previous sibling element of the node
150
cdef tree.xmlNode* nextElement(tree.xmlNode* c_node) nogil
151
cdef tree.xmlNode* previousElement(tree.xmlNode* c_node) nogil
153
##########################################################################
154
# iterators (deprecated API, don't use in new code)
156
cdef class lxml.etree._ElementTagMatcher [ object LxmlElementTagMatcher ]:
160
# store "{ns}tag" (or None) filter for this matcher or element iterator
161
# ** unless _href *and* _name are set up 'by hand', this function *must*
162
# ** be called when subclassing the iterator below!
163
cdef void initTagMatch(_ElementTagMatcher matcher, tag)
165
cdef class lxml.etree._ElementIterator(_ElementTagMatcher) [
166
object LxmlElementIterator ]:
168
cdef tree.xmlNode* (*_next_element)(tree.xmlNode*)
170
# store the initial node of the iterator if it matches the required tag
171
# or its next matching sibling if not
172
cdef void iteratorStoreNext(_ElementIterator iterator, _Element node)
174
##########################################################################
175
# other helper functions
177
# check if a C node matches a tag name and namespace
178
# (NULL allowed for each => always matches)
179
cdef int tagMatches(tree.xmlNode* c_node, const_xmlChar* c_href, const_xmlChar* c_name)
181
# convert a UTF-8 char* to a Python string or unicode string
182
cdef object pyunicode(const_xmlChar* s)
184
# convert the string to UTF-8 using the normal lxml.etree semantics
185
cdef bytes utf8(object s)
187
# split a tag into a (URI, name) tuple, return None as URI for '{}tag'
188
cdef tuple getNsTag(object tag)
190
# split a tag into a (URI, name) tuple, return b'' as URI for '{}tag'
191
cdef tuple getNsTagWithEmptyNs(object tag)
193
# get the "{ns}tag" string for a C node
194
cdef object namespacedName(tree.xmlNode* c_node)
196
# get the "{ns}tag" string for a href/tagname pair (c_ns may be NULL)
197
cdef object namespacedNameFromNsName(const_xmlChar* c_ns, const_xmlChar* c_tag)
199
# check if the node has a text value (which may be '')
200
cdef bint hasText(tree.xmlNode* c_node) nogil
202
# check if the node has a tail value (which may be '')
203
cdef bint hasTail(tree.xmlNode* c_node) nogil
205
# get the text content of an element (or None)
206
cdef object textOf(tree.xmlNode* c_node)
208
# get the tail content of an element (or None)
209
cdef object tailOf(tree.xmlNode* c_node)
211
# set the text value of an element
212
cdef int setNodeText(tree.xmlNode* c_node, text) except -1
214
# set the tail text value of an element
215
cdef int setTailText(tree.xmlNode* c_node, text) except -1
217
# append an element to the children of a parent element
218
cdef void appendChild(_Element parent, _Element child)
220
# recursively lookup a namespace in element or ancestors, or create it
221
cdef tree.xmlNs* findOrBuildNodeNsPrefix(
222
_Document doc, tree.xmlNode* c_node, const_xmlChar* href, const_xmlChar* prefix)
224
# find the Document of an Element, ElementTree or Document (itself!)
225
cdef _Document documentOrRaise(object input)
227
# find the root Element of an Element (itself!), ElementTree or Document
228
cdef _Element rootNodeOrRaise(object input)