TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/strophe/strophejs/llms.txt
Use this file to discover all available pages before exploring further.
stx tagged template literal and the Stanza class it returns offer a modern, declarative way to construct XMPP stanzas. Instead of chaining method calls as with Builder, you write XML inline as a template literal and embed dynamic values directly with ${...} interpolation. All interpolated string values are automatically XML-escaped, so user-supplied content cannot inject malformed XML or break stanza structure. The stx API was introduced in Strophe.js 3.1.0 and is the recommended approach when you prefer readable inline XML over the fluent builder pattern.
stx Tagged Template Literal
stx function is a tagged template literal that turns an inline XML template into a Stanza instance. It is exported from strophe.js and registered on globalThis as stx.
How it works
Each${value} slot in the template is processed according to its type before the final XML string is assembled:
| Value type | Behaviour |
|---|---|
string | XML-escaped (e.g. < → <) |
number | Coerced to string, then XML-escaped |
null / undefined | Treated as an empty string ("") |
Stanza | Serialized via .toString() — not re-escaped |
Builder | Serialized via .toString() — not re-escaped |
UnsafeXML | Embedded verbatim — not escaped |
StanzaValue[] | Each element processed by the same rules |
Only
string (and number) values are escaped. Nested Stanza and Builder objects are trusted to already produce valid XML. If you must embed a raw XML string, use Stanza.unsafeXML() to mark it explicitly as trusted.Parameters
The static string fragments of the template literal, provided automatically by the JavaScript engine.
The interpolated values, each of which is
string | Stanza | Builder | UnsafeXML | StanzaValue[].A new
Stanza instance that lazily builds its DOM tree on first access.Basic usage
Multi-line stanza
Nested Stanza interpolation
You can embed one stx-produced stanza inside another. The inner stanza’s XML is inserted verbatim without re-escaping:
Array of stanzas
Interpolating an array ofStanza or Builder objects concatenates their XML representations in order:
StanzaValue Type
stx templates. Recursive arrays allow you to spread lists of child stanzas naturally.
Stanza Class
Stanza extends Builder and represents an XML stanza constructed from a tagged template literal. You will not normally instantiate Stanza directly — use the stx tag function instead.
Constructor
Do not call the
Stanza constructor directly. Use the stx tagged template literal, which calls this constructor for you.The static string segments of the template, supplied automatically by the JavaScript runtime.
The dynamic values interpolated into the template.
toString()
The complete XML string representation of the stanza, trimmed of leading/trailing whitespace.
buildTree()
Element for this stanza. Internally it calls Stanza.toElement(this.toString(), true), so an invalid XML string or wrong namespace will throw immediately.
The root DOM
Element of the parsed stanza.Stanza.toElement(string, throwErrorIfInvalidNS?)
Element. For <message/>, <iq/>, and <presence/> elements it also validates that the namespace is jabber:client or jabber:server; a wrong namespace either logs an error or throws, depending on the second argument.
A well-formed XML string to parse. Throws a
Parser Error if the XML is invalid.When
true, an invalid namespaceURI on a core stanza element throws an Error instead of logging. Defaults to undefined (falsy — logs only).The first element child of the parsed document, with leading/trailing whitespace-only text nodes stripped.
Stanza.unsafeXML(string)
UnsafeXML marker object. When an UnsafeXML value is interpolated into an stx template, it is embedded verbatim — no escaping is applied.
A raw XML string to embed without escaping. Must be valid XML in the context where it is used.
An
UnsafeXML instance (a subclass of String) that signals to stx to skip escaping.UnsafeXML Class
UnsafeXML is a thin subclass of the built-in String type. Its sole purpose is to carry a type-level marker that tells stx to embed the string without escaping. Instances are created exclusively through Stanza.unsafeXML() — do not call new UnsafeXML(...) directly.
toStanza Utility Function
toStanza is a convenience alias for Stanza.toElement, exported from strophe.js and registered on globalThis. Use it when you need to parse a raw XML string into a DOM element outside of a stx template.