Doc
objectAn XML document: get its root element, build it up, convert it to text, transform it with XSLT and write it to a file. A document holds exactly one root element. Functions that transform it (`xslt`, `removeNamespaces`) return a new document and leave this one unchanged.
- Kind Object
- Library Xml
- Category APIs & Data
- Functions 6
How to get one
local doc = Xml.create()Returned by
Leads to
Functions
-
rootEl Returns the root element. -
elementEl Creates the root element of the document and returns it. -
toStringstring Returns the document as indented XML text, starting with an XML declaration. -
writeself Writes the document to a file as indented XML with an XML declaration, replacing the file. -
xsltDoc Applies an XSLT 1.0 stylesheet and returns the result as a new document; this document is not changed. -
removeNamespacesDoc Returns a new document without namespaces, which makes values easy to read from SOAP responses.
root()
Section titled “root()”doc.root()→El Returns the root element. Throws when the document has no root yet.
Returns
element()
Section titled “element()”doc.element(name: string)→Eldoc.element(name: string, uri: string)→Eldoc.element(name: string, prefix: string, uri: string)→El Creates the root element of the document and returns it. A root that was already there is replaced, together with everything below it. To add a child to an element use `El.element`. A single namespace argument is either a URI (the element is in that default namespace) or a prefix and URI in one string separated by a comma and a space (`soap, http://schemas.xmlsoap.org/soap/envelope/`).
Parameters
-
namestring - Name of the root element
-
prefixstring - Namespace prefix
-
uristring - Namespace URI
Returns
toString()
Section titled “toString()”doc.toString()→string Returns the document as indented XML text, starting with an XML declaration.
Returns
write()
Section titled “write()”doc.write(fileName: string)→self Writes the document to a file as indented XML with an XML declaration, replacing the file. Missing parent directories in the workspace are created, and a bare file name like `doc.xml` works.
Parameters
-
fileNamestring - File to write, relative to the workspace
Returns
Example runs in CI
local doc = Xml.read("<greeting>hello</greeting>")doc.write("out/greeting.xml")print(Fl.read("out/greeting.xml")[2])xslt()
Section titled “xslt()”doc.xslt(xsl: string)→Doc Applies an XSLT 1.0 stylesheet and returns the result as a new document; this document is not changed. The result of the transformation must itself be well-formed XML. Throws `Failed to remove namespaces from xml` (the text is shared with `removeNamespaces`) when the stylesheet or the transformation is invalid.
Parameters
-
xslstring - XSLT stylesheet as text
Returns
Example runs in CI
local doc = Xml.read("<items><item>1</item><item>2</item></items>")local stylesheet = [[<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="/"><count><xsl:value-of select="count(//item)"/></count></xsl:template></xsl:stylesheet>]]print(doc.xslt(stylesheet).root().value())removeNamespaces()
Section titled “removeNamespaces()”doc.removeNamespaces()→Doc Returns a new document without namespaces, which makes values easy to read from SOAP responses. Elements keep only their local name, elements in the SOAP envelope namespace (`http://schemas.xmlsoap.org/soap/envelope/`, so Envelope, Header and Body) are replaced by their content, `xsi:nil` attributes are dropped and every other attribute is converted into a child element with the attribute's name and value. This document is not changed.
Returns
Example runs in CI
local response = Xml.read("<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/' xmlns:n='urn:n'><s:Body><n:Result><n:total>42</n:total></n:Result></s:Body></s:Envelope>")print(response.removeNamespaces().root().child("total").value())