Skip to content

Xml2Node

[Source]

Wrapper around a libxml2 xmlNode pointer, providing helpers for XPath evaluation, attribute/content access, and basic tree navigation.

class ref Xml2Node

Constructors

fromPTR

[Source]

Create an Xml2Node from a non-null libxml2 xmlNode*.

  • ptrx: Nullable pointer to an XmlNode.

Raises an error if ptrx is None, otherwise stores the pointer in ptr', assigns the underlying XmlNode to ptr, and marks the node as allocated.

new ref fromPTR(
  xml2doc': Xml2Doc tag,
  ptrx: NullablePointer[XmlNode ref] ref)
: Xml2Node ref^ ?

Parameters

Returns


Public fields

var ptr': NullablePointer[XmlNode ref] ref

[Source]


var ptr: XmlNode ref

[Source]


var xml2doc: Xml2Doc tag

[Source]


Public Functions

xpathEval

[Source]

Evaluate an XPath expression relative to this node as the context node.

  • xpath: The XPath expression to evaluate.
  • namespaces: Optional list of (prefix, uri) pairs to register on a temporary XPath context before evaluation.

Internally, creates a new xmlXPathContext for the owning document, registers the provided namespaces, sets this node as the context node, calls xmlXPathEval, frees the context, and returns an Xml2XPathResult for the resulting xmlXPathObject*.

fun box xpathEval(
  xpath: String val,
  namespaces: Array[(String val , String val)] ref = array)
: (None val | Array[Xml2Node ref] ref | Bool val | 
    String val | F64 val)

Parameters

Returns


xpathEvalNodes

[Source]

A convenience method that calls xpathEval and returns an Array[Xml2Node].

fun box xpathEvalNodes(
  xpath: String val,
  namespaces: Array[(String val , String val)] ref = array)
: Array[Xml2Node ref] ref ?

Parameters

Returns


xpathEvalString

[Source]

A convenience method that calls xpathEval and returns a String val.

fun box xpathEvalString(
  xpath: String val,
  namespaces: Array[(String val , String val)] ref = array)
: String val ?

Parameters

Returns


xpathEvalF64

[Source]

A convenience method that calls xpathEval and returns an F64 (XML's default Number type in libxml2).

fun box xpathEvalF64(
  xpath: String val,
  namespaces: Array[(String val , String val)] ref = array)
: F64 val ?

Parameters

Returns


xpathEvalBool

[Source]

A convenience method that calls xpathEval and returns a Bool.

fun box xpathEvalBool(
  xpath: String val,
  namespaces: Array[(String val , String val)] ref = array)
: Bool val ?

Parameters

Returns


name

[Source]

Return this node’s name as a Pony String.

The name is obtained from the underlying xmlNode->name C string and cloned into a Pony-managed string.

fun ref name()
: String val

Returns


getLineNo

[Source]

Return the line number in the source document where this node was parsed, or -1 if the information is unavailable.

This forwards to xmlGetLineNo on the underlying node.

fun ref getLineNo()
: I64 val

Returns


getNodePath

[Source]

Return an XPath-like string representing the absolute path of this node within the document tree.

This is the result of libxml2’s xmlGetNodePath helper.

fun ref getNodePath()
: String val

Returns


xpathCastNodeToString

[Source]

Cast this node to a string using libxml2’s XPath string conversion rules (equivalent to XPath’s string() function applied to the node).

Delegates to xmlXPathCastNodeToString on the underlying node.

fun ref xpathCastNodeToString()
: String val

Returns


getProps

[Source]

fun ref getProps()
: Array[(String val , String val)] ref

Returns


getProp

[Source]

Get the value of an attribute on this element node.

  • pname: Attribute name (without any prefix/namespace handling).

Returns the attribute value string from xmlGetProp, or an empty string if the attribute is not present.

fun ref getProp(
  pname: String val)
: String val

Parameters

Returns


getContent

[Source]

Return the textual content of this node, including text from its descendants as defined by libxml2’s xmlNodeGetContent.

Useful for retrieving the logical text value of elements or text nodes.

fun ref getContent()
: String val

Returns


getLang

[Source]

Return the xml:lang value in scope for this node.

This consults the node and its ancestors using xmlNodeGetLang.

fun ref getLang()
: String val

Returns


getChildren

[Source]

Return all child element nodes of this node as an array of Xml2Node.

Uses xmlChildElementCount, xmlFirstElementChild, and xmlNextElementSibling to iterate only over element children, skipping text, comments, and other non-element node types. If there are no element children, returns an empty array.

fun ref getChildren()
: Array[Xml2Node ref] ref

Returns


setProp

[Source]

Creates or Sets a property value (think XML Attribute) on this node.

fun ref setProp(
  pname: String val,
  pvalue: String val)
: None val

Parameters

Returns


unsetProp

[Source]

Unsets a property (think XML Attribute) on this node.

fun ref unsetProp(
  pname: String val)
: None val

Parameters

Returns


nodeDump

[Source]

Serialize this node and its subtree to a string using libxml2's xmlNodeDump.

  • plevel: Indentation level to start from when pretty-printing.
  • pformat: Non-zero to enable formatted (indented) output, zero for unformatted.

Internally, creates a temporary xmlBuffer, dumps the node into it, and returns the buffer's contents as a Pony String.

fun ref nodeDump(
  plevel: I32 val,
  pformat: I32 val)
: String val

Parameters

  • plevel: I32 val
  • pformat: I32 val

Returns


appendChild

[Source]

Add a child node to this element.

  • child: Node to add as child

Returns the added child node. The child is added at the end of the children list. Raises error if the operation fails.

Example:

let parent = doc.createElement("parent")?
let child = doc.createElement("child")?
parent.appendChild(child)?

fun ref appendChild(
  child: Xml2Node ref)
: Xml2Node ref ?

Parameters

Returns


setContent

[Source]

Set the text content of this node.

  • content: Text content to set

Replaces any existing content of the node. For elements with children, this will replace all children with a single text node.

Example:

let elem = doc.createElement("item")?
elem.setContent("New content")

fun ref setContent(
  content: String val)
: None val

Parameters

Returns


addChild

[Source]

Convenience method to create and add a child element in one step.

  • child_name: Element name for the new child
  • content: Optional text content

Creates a new child element, adds it to this node, and returns the new child wrapped as Xml2Node.

Example:

let doc = Xml2Doc.createWithRoot("root")?
let root = doc.getRootElement()?
let child1 = root.addChild("item", "Hello")?
let child2 = root.addChild("item", "World")?
child1.setProp("id", "1")
child2.setProp("id", "2")

fun ref addChild(
  child_name: String val,
  content: String val = "")
: Xml2Node ref ?

Parameters

Returns