El
objectAn XML element: read its text and attributes, move to its children and parent, and add or remove content. Editing functions that return the element itself (`text`, `attribute`, `namespace`, `detach`) can be chained. Namespace arguments are a URI or a prefix and URI in one string (`o, urn:orders`).
- Kind Object
- Library Xml
- Category APIs & Data
- Functions 12
How to get one
local doc = Xml.create()local el = doc.root()Returned by
Functions
-
rootEl Returns the element you started from with `Doc.root` or `Doc.element`, the top of this tree. -
upEl Returns the parent element, so you can step back after `child`, `children` or `element`. -
stringstring Returns this element and everything below it as compact XML text (no indentation). -
valuestring Returns the text of the element: its own text and that of all elements below it, without tags. -
attributeValuestring Returns the value of an attribute. -
childEl Returns the first child element with the given name. -
childrenEl[] Returns the child elements as a list, in document order: all children, or only those with the given name. -
elementEl Creates a new element, appends it as the last child of this element and returns it, so `text` and `attribute` can be chained on it. -
textself Appends text to the element, after any text and children it already has. -
attributeself Sets an attribute, replacing an attribute with the same name. -
namespaceself Declares a namespace on the element (an `xmlns` attribute). -
detachself Removes the element from its parent.
root()
Section titled “root()”el.root()→El Returns the element you started from with `Doc.root` or `Doc.element`, the top of this tree.
Returns
Returns the parent element, so you can step back after `child`, `children` or `element`. Only elements reached through those functions know their parent: on the element from `Doc.root` or `Doc.element` (the top of the tree) `up()` returns nil. With a number the call climbs that many levels (`up(2)` is `up().up()`) and throws `Can't get upper element` when the top is reached before that. The number must be above 0.
Parameters
-
amountnumber optional - Number of levels to climb, above 0 (see description)
Returns
string()
Section titled “string()”el.string()→string Returns this element and everything below it as compact XML text (no indentation).
Returns
value()
Section titled “value()”el.value()→string Returns the text of the element: its own text and that of all elements below it, without tags.
Returns
attributeValue()
Section titled “attributeValue()”el.attributeValue(name: string)→stringel.attributeValue(name: string, ns: string)→string Returns the value of an attribute. An attribute that is not there gives an empty string, so a missing attribute cannot be told apart from an empty one.
Parameters
-
namestring - Attribute name
-
nsstring optional - Namespace of the attribute: a URI, or `prefix, uri`
Returns
child()
Section titled “child()”Returns the first child element with the given name. Without a namespace argument only children that are in no namespace match; give the namespace for namespaced children. Returns nil when there is no such child, so an optional child can be tested with `if child then`.
Parameters
-
childstring - Name of the child element
-
nsstring optional - Namespace of the child: a URI, or `prefix, uri`
Returns
Example runs in CI
local doc = Xml.read("<order><customer>Alice</customer><item>Widget</item></order>")local order = doc.root()print(order.child("customer").value())print(#order.children("discount"))children()
Section titled “children()”Returns the child elements as a list, in document order: all children, or only those with the given name. The list is empty when there are none. Without a namespace argument only children that are in no namespace match; pass the namespace to find namespaced children.
Parameters
-
childstring optional - Name of the child elements
-
nsstring optional - Namespace of the children: a URI, or `prefix, uri`
Returns
Example runs in CI
local doc = Xml.read("<orders><order id='1'>A</order><order id='2'>B</order></orders>")for _, order in ipairs(doc.root().children("order")) do print(order.attributeValue("id"), order.value())endelement()
Section titled “element()”el.element(name: string)→Elel.element(name: string, ns: string)→Elel.element(name: string, prefix: string, uri: string)→El Creates a new element, appends it as the last child of this element and returns it, so `text` and `attribute` can be chained on it. Use `up` to come back to this element. A single namespace argument is a URI or `prefix, uri`; the prefix and URI can also be passed as two arguments.
Parameters
-
namestring - Name of the new element
-
prefixstring - Namespace prefix
-
uristring - Namespace URI
-
nsstring - Namespace of the element: a URI, or `prefix, uri`
Returns
text()
Section titled “text()”el.text(content: string)→self Appends text to the element, after any text and children it already has. The text is escaped.
Parameters
-
contentstring - Text to append
Returns
attribute()
Section titled “attribute()”el.attribute(name: string, value: string)→selfel.attribute(name: string, value: string, uri: string)→self Sets an attribute, replacing an attribute with the same name. With a namespace the attribute is put in that namespace: pass `prefix, uri`, or just a URI, in which case a prefix that is already declared for the URI is used, or a new one (`ns1`, `ns2`, ...).
Parameters
-
namestring - Name of the attribute
-
valuestring - Value of the attribute
-
uristring optional - Namespace of the attribute: a URI, or `prefix, uri`
Returns
namespace()
Section titled “namespace()”el.namespace(ns: string)→selfel.namespace(prefix: string, uri: string)→self Declares a namespace on the element (an `xmlns` attribute). With one argument, a URI declares the default namespace and `prefix, uri` a prefixed one; with two arguments the prefix and URI are given separately. The declaration does not move the element itself into the namespace.
Parameters
-
prefixstring - Namespace prefix
-
uristring - Namespace URI
-
nsstring - Namespace to declare: a URI, or `prefix, uri`
Returns
detach()
Section titled “detach()”el.detach()→self Removes the element from its parent. The detached element is returned and can still be read or added elsewhere.
Returns