xmlpatch

package module
v0.0.0-...-5847327 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2024 License: MIT Imports: 5 Imported by: 0

README

go-xml-patch

A golang implementation of RFC 5261: "An Extensible Markup Language (XML) Patch Operations Framework Utilizing XML Path Language (XPath) Selectors".

Status

This library is still in early stage. API may break. Missing functionality (see progress). Probably buggy 🐛.

Usage

Install
go get github.com/maxyurk/go-xml-patch
Code
package main

import (
	"fmt"
	"github.com/maxyurk/go-xml-patch"
	"os"
)

func main() {
	target, _ := os.ReadFile("target.xml")
	diff, _ := os.ReadFile("diff.xml")
	patch, err := xmlpatch.Patch(target, diff)
	if err != nil {
		panic(err)
	}
	fmt.Println(patch)
}

Example from RFC 5261

An example target XML document:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <foo a="1">This is a sample document</foo>
</doc>

An XML diff document:

<?xml version="1.0" encoding="UTF-8"?>
<diff>
    <replace sel="doc/foo[@a='1']">
        <bar a="2"/>
    </replace>
</diff>

A result XML document:

<?xml version="1.0" encoding="UTF-8"?>
<doc>
    <bar a="2"/>
</doc>

Progress

Specification Items
  • 4.3. <add> Element
    • 4.3.1. Adding an Element
    • 4.3.2. Adding an Attribute
    • 4.3.3. Adding a Prefixed Namespace Declaration
    • 4.3.4. Adding Node(s) with the 'pos' Attribute
    • 4.3.5. Adding Multiple Nodes
  • 4.4. <replace> Element (with optional auto-create, see #2)
    • 4.4.1. Replacing an Element
    • 4.4.2. Replacing an Attribute Value
    • 4.4.3. Replacing a Namespace Declaration URI
    • 4.4.4. Replacing a Comment Node
    • 4.4.5. Replacing a Processing Instruction Node
    • 4.4.6. Replacing a Text Node
  • 4.5. <remove> Element
    • 4.5.1. Removing an Element
    • 4.5.2. Removing an Attribute
    • 4.5.3. Removing a Prefixed Namespace Declaration
    • 4.5.4. Removing a Comment Node
    • 4.5.5. Removing a Processing Instruction Node
    • 4.5.6. Removing a Text Node
Other
  • CI
  • Release flow
  • better docs
  • coverage

Release

(manual for now)

Follow Go module publishing instructions and then create a new Github release.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Patch

func Patch(docData, xmlDiffData []byte, options ...Ops) ([]byte, error)

Types

type Add

type Add struct {
	Pos     string `xml:"pos,attr"`
	Sel     string `xml:"sel,attr"`
	Content []byte `xml:",innerxml"`
}

type Diff

type Diff struct {
	XMLName  xml.Name  `xml:"diff"`
	Replaces []Replace `xml:"replace"`
	Adds     []Add     `xml:"add"`
}

type Ops

type Ops int
const (
	ReplaceAutoCreateMissing Ops = iota + 1
)

type Replace

type Replace struct {
	Sel     string `xml:"sel,attr"`
	Text    string `xml:",chardata"`
	Content []byte `xml:",innerxml"`
}

Jump to

Keyboard shortcuts

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