xpathutils

package module
v0.1.0 Latest Latest
Warning

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

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

README

Go XPath Utilities

A utility library for manipulating XML documents with XPath in Go.

Powered by xmlquery.

Table of contents

Features

Supports the following XPath operations:

  • Retrieve an attribute's value
  • Add, update, or remove an attribute
  • Remove a node

Note: Nodes cannot be retrieved, added, or updated via XPath. This simplifies the utility, as add and update operations require a string value.

Installation

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

Usage

Get attribute by XPath

Get an attribute by XPath:

// Get the value of the "buzz" attribute in the first <fizz> element in foo.xml
value, err := xpathutils.GetAttrByXPathFromFile("foo.xml", "", "//fizz/@buzz")
// value = "fizzbuzz"

// For XML strings, use:
value, err := xpathutils.GetAttrByXPathFromString(xmlStr, "//fizz/@buzz")
Add attribute by XPath

Add an attribute by XPath:

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

// For XML strings, use:
updatedXML, err := xpathutils.AddAttrByXPathFromString(xmlStr, "//fizz/@buzz", "fizzbuzz")
Update attribute by XPath

Update an attribute by XPath:

// Update the "buzz" attribute in the first <fizz> element in foo.xml to "xyz"
err := xpathutils.UpdateAttrByXPathFromFile("foo.xml", "", "//fizz/@buzz", "xyz")

// For XML strings, use:
updatedXML, err := xpathutils.UpdateAttrByXPathFromString(xmlStr, "//fizz/@buzz", "xyz")
Remove by XPath

Remove an attribute by XPath:

/// Remove the "buzz" attribute from the first <fizz> element in foo.xml
err := xpathutils.RemoveAttrByXPathFromFile("foo.xml", "", "//fizz/@buzz")

// For XML strings, use:
updatedXML, err := xpathutils.RemoveAttrByXPathFromString(xmlStr, "//fizz/@buzz")

Remove an element by XPath:

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

// For XML strings, use:
updatedXML, err := xpathutils.RemoveNodeByXPathFromString(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 AddAttrByXPathFromFile added in v0.1.0

func AddAttrByXPathFromFile(inputFile, outputFile, expr, value string) error

AddAttrByXPathFromFile adds an attribute at the XPath location in the XML document.

func AddAttrByXPathFromString added in v0.1.0

func AddAttrByXPathFromString(xmlStr, expr, value string) (string, error)

AddAttrByXPathFromString adds an attribute at the XPath location in the given XML string.

func GetAttrByXPathFromFile added in v0.1.0

func GetAttrByXPathFromFile(inputFile, expr string) (string, error)

GetAttrByXPathFromFile retrieves the value of an attribute at the specified XPath in the XML document.

func GetAttrByXPathFromString added in v0.1.0

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

GetAttrByXPathFromString retrieves the value of an attribute at the specified XPath in the given XML string.

func RemoveAttrByXPathFromFile added in v0.1.0

func RemoveAttrByXPathFromFile(inputFile, outputFile, expr string) error

RemoveAttrByXPathFromFile removes an attribute at the specified XPath from the XML document.

func RemoveAttrByXPathFromString added in v0.1.0

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

RemoveAttrByXPathFromString removes an attribute at the specified XPath from the given XML string.

func RemoveNodeByXPathFromFile added in v0.1.0

func RemoveNodeByXPathFromFile(inputFile, outputFile, expr string) error

RemoveNodeByXPathFromFile removes an attribute at the specified XPath from the XML document.

func RemoveNodeByXPathFromString added in v0.1.0

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

RemoveNodeByXPathFromString removes an attribute at the specified XPath from the given XML string.

func UpdateAttrByXPathFromFile added in v0.1.0

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

UpdateAttrByXPathFromFile updates the value of an attribute at the specified XPath in the XML document.

func UpdateAttrByXPathFromString added in v0.1.0

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

UpdateAttrByXPathFromString updates the value of an attribute at the specified XPath in the given XML string.

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