Skip to content

An 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.

How to get one

  1. Xml .create()
  2. Doc
local doc = Xml.create()

Functions

  • root El Returns the root element.
  • element El Creates the root element of the document and returns it.
  • toString string Returns the document as indented XML text, starting with an XML declaration.
  • write self Writes the document to a file as indented XML with an XML declaration, replacing the file.
  • xslt Doc Applies an XSLT 1.0 stylesheet and returns the result as a new document; this document is not changed.
  • removeNamespaces Doc Returns a new document without namespaces, which makes values easy to read from SOAP responses.
doc.root()→El

Returns the root element. Throws when the document has no root yet.

Returns

El An XML element: read its text and attributes, move to its children and parent, and add or remove content.
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

name string
Name of the root element
prefix string
Namespace prefix
uri string
Namespace URI

Returns

El An XML element: read its text and attributes, move to its children and parent, and add or remove content.
doc.toString()→string

Returns the document as indented XML text, starting with an XML declaration.

Returns

string The XML text
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

fileName string
File to write, relative to the workspace

Returns

self chainable The doc itself, so calls can be chained.

Example runs in CI

local doc = Xml.read("<greeting>hello</greeting>")
doc.write("out/greeting.xml")
print(Fl.read("out/greeting.xml")[2])
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

xsl string
XSLT stylesheet as text

Returns

Doc An XML document: get its root element, build it up, convert it to text, transform it with XSLT and write it to a file.

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())
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

Doc An XML document: get its root element, build it up, convert it to text, transform it with XSLT and write it to a file.

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())