Crate xot

source ·
Expand description

Xot is an XML library that lets you access manipulate XML documents as a tree in memory.

use xot::Xot;

let mut xot = Xot::new();

let root = xot.parse("<p>Example</p>")?;
let doc_el = xot.document_element(root)?;
let txt = xot.first_child(doc_el).unwrap();
let txt_value = xot.text_mut(txt).unwrap();
txt_value.set("Hello, world!");

assert_eq!(xot.to_string(root)?, "<p>Hello, world!</p>");

§Xot approach

Xot exposes a single Xot struct that you use to access, create and manipulate all your XML data. Multiple XML trees can exist in an Xot struct at the same time, and you’re free to move nodes between these trees.

Node is a lightweight handle to a node in the XML tree that you use with Xot for both access and manipulation. To navigate the tree use accessors such as Xot::first_child or iterators such a Xot::children. You then use operations such as Xot::append to manipulate the tree.

To access and manipulate XML specific data, you use the Value for a node. This is an enum that’s either an Element, Text, Comment or ProcessingInstruction, Attribute, Namespace or Document (which has no value). You can use Xot::value to get the Value. Sometimes it’s more handy to use the specific accessors for a value, such a Xot::element or Xot::text.

XML names and namespaces in Xot are referenced by ids. In order to construct or compare an element, you first need to get hold of a name. To access a name, use Xot::name. To create a new name if necessary, use Xot::add_name. To construct a name with a namespace, use Xot::add_namespace and then Xot::add_name_ns. To create a namespace prefix, use Xot::add_prefix. You can also use the xmlname module to manage names; see xmlname::CreateName for a bunch of convenient ways to create names, for instance.

Attributes and namespace access is most conveniently done through the Xot::attributes and Xot::namespaces accessors. Manipulation is most conveniently done through their mutable variants Xot::attributes_mut and Xot::namespaces_mut.

In some cases however you may want to be able to create namespace and attribute nodes directly. This can be done through the Xot::new_namespace_node and Xot::new_attribute_node APIs.

You can also create Xot nodes from a fixed structure, the fixed submodule.


  • A fixed representation of a tree of nodes.
  • Xot offers functionality to serialize XML data in different ways.
  • Proptest support for Xot
  • This module allows you to use XML names in various ways.


  • Represents an attribute node.
  • XML comment.
  • XML element name.
  • Serialize a Xot node to HTML5
  • A MutableNodeMap is a struct with a hash-map like API and is used to expose attribute and namespace prefix information in a mutable way.
  • Id uniquely identifying a name and namespace.
  • Represents a namespace node.
  • Id uniquely identifying namespace.
  • A node in the XML tree. This is a lightweight value and can be copied.
  • A NodeMap is a struct with a hash-map like API and is used to expose attribute and namespace prefix information.
  • Id uniquely identifying a prefix
  • XML processing instruction value.
  • A span with a start and end position
  • Span information for a parsed XML document.
  • XML text value.
  • The Xot struct manages all XML tree data in your program. It lets you access and manipulate one or more XML documents and fragments.


Type Aliases§