Files
herolib/aiprompts/v_core/encoding/xml.md
2025-09-02 07:28:13 +02:00

8.1 KiB

module xml

Contents

Constants

const default_entities = {
	'lt':   '<'
	'gt':   '>'
	'amp':  '&'
	'apos': "'"
	'quot': '"'
}

[Return to contents]

const default_entities_reverse = {
	'<': 'lt'
	'>': 'gt'
	'&': 'amp'
	"'": 'apos'
	'"': 'quot'
}

[Return to contents]

escape_text

fn escape_text(content string, config EscapeConfig) string

escape_text replaces all entities in the given string with their respective XML entity strings. See default_entities, which can be overridden.

[Return to contents]

parse_single_node

fn parse_single_node(first_char u8, mut reader io.Reader) !XMLNode

parse_single_node parses a single XML node from the reader. The first character of the tag is passed in as the first_char parameter. This function is meant to assist in parsing nested nodes one at a time. Using this function as opposed to the recommended static functions makes it easier to parse smaller nodes in extremely large XML documents without running out of memory.

[Return to contents]

unescape_text

fn unescape_text(content string, config UnescapeConfig) !string

unescape_text replaces all entities in the given string with their respective original characters or strings. See default_entities_reverse, which can be overridden.

[Return to contents]

XMLDocument.from_file

fn XMLDocument.from_file(path string) !XMLDocument

XMLDocument.from_file parses an XML document from a file. Note that the file is read in its entirety and then parsed. If the file is too large, try using the XMLDocument.from_reader function instead.

[Return to contents]

XMLDocument.from_reader

fn XMLDocument.from_reader(mut reader io.Reader) !XMLDocument

XMLDocument.from_reader parses an XML document from a reader. This is the most generic way to parse an XML document from any arbitrary source that implements that io.Reader interface.

[Return to contents]

XMLDocument.from_string

fn XMLDocument.from_string(raw_contents string) !XMLDocument

XMLDocument.from_string parses an XML document from a string.

[Return to contents]

DTDListItem

type DTDListItem = DTDElement | DTDEntity

[Return to contents]

XMLNodeContents

type XMLNodeContents = XMLCData | XMLComment | XMLNode | string

[Return to contents]

DTDElement

struct DTDElement {
pub:
	name       string   @[required]
	definition []string @[required]
}

[Return to contents]

DTDEntity

struct DTDEntity {
pub:
	name  string @[required]
	value string @[required]
}

[Return to contents]

DocumentType

struct DocumentType {
pub:
	name string @[required]
	dtd  DTDInfo
}

[Return to contents]

DocumentTypeDefinition

struct DocumentTypeDefinition {
pub:
	name string
	list []DTDListItem
}

[Return to contents]

EscapeConfig

struct EscapeConfig {
pub:
	reverse_entities map[string]string = default_entities_reverse
}

[Return to contents]

UnescapeConfig

struct UnescapeConfig {
pub:
	entities map[string]string = default_entities
}

[Return to contents]

XMLCData

struct XMLCData {
pub:
	text string @[required]
}

[Return to contents]

XMLComment

struct XMLComment {
pub:
	text string @[required]
}

[Return to contents]

XMLDocument

struct XMLDocument {
	Prolog
pub:
	root XMLNode @[required]
}

XMLDocument is the struct that represents a single XML document. It contains the prolog and the single root node. The prolog struct is embedded into the XMLDocument struct, so that the prolog fields are accessible directly from the this struct. Public prolog fields include version, enccoding, comments preceding the root node, and the document type definition.

[Return to contents]

get_element_by_id

fn (doc XMLDocument) get_element_by_id(id string) ?XMLNode

get_element_by_id returns the first element with the given id, or none if no such element exists.

[Return to contents]

get_elements_by_attribute

fn (doc XMLDocument) get_elements_by_attribute(attribute string, value string) []XMLNode

get_elements_by_attribute returns all elements with the given attribute-value pair. If there are no such elements, an empty array is returned.

[Return to contents]

get_elements_by_tag

fn (doc XMLDocument) get_elements_by_tag(tag string) []XMLNode

get_elements_by_tag returns all elements with the given tag name. If there are no such elements, an empty array is returned.

[Return to contents]

pretty_str

fn (doc XMLDocument) pretty_str(indent string) string

pretty_str returns a pretty-printed version of the XML document. It requires the string used to indent each level of the document.

[Return to contents]

str

fn (doc XMLDocument) str() string

str returns a string representation of the XML document. It uses a 2-space indentation to pretty-print the document.

[Return to contents]

validate

fn (doc XMLDocument) validate() !XMLDocument

validate checks the document is well-formed and valid. It returns a new document with the parsed entities expanded when validation is successful. Otherwise it returns an error.

[Return to contents]

XMLNode

struct XMLNode {
pub:
	name       string @[required]
	attributes map[string]string
	children   []XMLNodeContents
}

XMLNode represents a single XML node. It contains the node name, a map of attributes, and a list of children. The children can be other XML nodes, CDATA, plain text, or comments.

[Return to contents]

get_element_by_id

fn (node XMLNode) get_element_by_id(id string) ?XMLNode

get_element_by_id returns the first element with the given id, or none if no such element exists in the subtree rooted at this node.

[Return to contents]

get_elements_by_attribute

fn (node XMLNode) get_elements_by_attribute(attribute string, value string) []XMLNode

get_elements_by_attribute returns all elements with the given attribute-value pair in the subtree rooted at this node. If there are no such elements, an empty array is returned.

[Return to contents]

get_elements_by_tag

fn (node XMLNode) get_elements_by_tag(tag string) []XMLNode

get_elements_by_tag returns all elements with the given tag name in the subtree rooted at this node. If there are no such elements, an empty array is returned.

[Return to contents]

pretty_str

fn (node XMLNode) pretty_str(original_indent string, depth int, reverse_entities map[string]string) string

pretty_str returns a pretty-printed version of the XML node. It requires the current indentation the node is at, the depth of the node in the tree, and a map of reverse entities to use when escaping text.

[Return to contents]

Powered by vdoc. Generated on: 2 Sep 2025 07:18:04