xpath

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2025 License: MIT Imports: 3 Imported by: 0

README

Go XPath Utilities

This project provides utility functions for working with XML documents using XPath expressions in Go.
It allows adding, updating, and removing elements or attributes in XML files or XML strings.

Installation

go get github.com/rmkane/go-xpath-utils

Usage

Add

Add an element by XPath:

// Add a <baz>zip</baz> element under the first <bar> element in foo.xml
err := xpath.AddByXPathFromFile("foo.xml", "", "//bar", "baz", "zip")

Add an attribute by XPath:

// Add a "buzz" attribute with value "fizzbuzz" to the first <fizz> element
err := xpath.AddByXPathFromFile("foo.xml", "", "//fizz/@buzz", "buzz", "fizzbuzz")
Update

Update an element value by XPath:

// Update the value of the first <bar> element in foo.xml to "baz"
err := xpath.UpdateByXPathFromFile("foo.xml", "", //bar", "baz")

Update an attribute by XPath:

// Update the "buzz" attribute in the first <fizz> element in foo.xml to "fizzbuzz"
err := xpath.UpdateByXPathFromFile("foo.xml", "", "//fizz/@buzz", "fizzbuzz")
Remove

Remove an element by XPath:

// Remove the first <bar> element from foo.xml
err := xpath.RemoveByXPathFromFile("foo.xml", "", "//bar")

Remove an attribute by XPath:

// Remove the "buzz" attribute from the first <fizz> element in foo.xml
err := xpath.RemoveByXPathFromFile("foo.xml", "", "//fizz/@buzz")
Working with Strings

These functions also support modifying XML strings instead of files:

updatedXML, err := xpath.AddByXPathFromString(xmlStr, "//bar", "baz", "zip")
updatedXML, err := xpath.UpdateByXPathFromString(xmlStr, "//bar", "baz")
updatedXML, err := xpath.RemoveByXPathFromString(xmlStr, "//bar")

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddByXPathFromFile

func AddByXPathFromFile(inputFile, outputFile, expr, key, value string) error

AddByXPathFromFile adds a node or attribute at the XPath location in the XML document. If the XPath targets an attribute, it will be added to the parent node. If the XPath targets an element, a new child node with the specified value will be created.

func AddByXPathFromString

func AddByXPathFromString(xmlStr, expr, key, value string) (string, error)

AddByXPathFromString adds a node or attribute at the XPath location in the given XML string. Returns the updated XML string or an error if the operation fails.

func RemoveByXPathFromFile

func RemoveByXPathFromFile(inputFile, outputFile, expr string) error

RemoveByXPathFromFile removes a node or attribute at the specified XPath from the XML document. If the XPath targets an attribute, it will be removed from the parent node. If the XPath targets an element, the node and its subtree will be removed.

func RemoveByXPathFromString

func RemoveByXPathFromString(xmlStr, expr string) (string, error)

RemoveByXPathFromString removes a node or attribute at the specified XPath from the given XML string. Returns the updated XML string or an error if the operation fails.

func UpdateByXPathFromFile

func UpdateByXPathFromFile(inputFile, outputFile, expr, newValue string) error

UpdateByXPathFromFile updates the value of a node or attribute at the specified XPath in the XML document. If the XPath targets an attribute, its value is updated. If the XPath targets an element, its content is replaced.

func UpdateByXPathFromString

func UpdateByXPathFromString(xmlStr, expr, newValue string) (string, error)

UpdateByXPathFromString updates the value of a node or attribute at the specified XPath in the XML string. Returns the updated XML string or an error if the operation fails.

Types

This section is empty.

Directories

Path Synopsis
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL