diff --git a/pkg/ottl/ottlfuncs/README.md b/pkg/ottl/ottlfuncs/README.md index 10f5acd833be..2820f5e17884 100644 --- a/pkg/ottl/ottlfuncs/README.md +++ b/pkg/ottl/ottlfuncs/README.md @@ -1293,7 +1293,7 @@ Examples: The `RemoveXML` Converter returns an edited version of an XML string with selected elements removed. `target` is a Getter that returns a string. This string should be in XML format. -If `target` is not a string, nil, or cannot be parsed as XML, `ParseXML` will return an error. +If `target` is not a string, nil, or is not valid xml, `RemoveXML` will return an error. `xpath` is a string that specifies an [XPath](https://www.w3.org/TR/1999/REC-xpath-19991116/) expression that selects one or more elements to remove from the XML document. diff --git a/pkg/ottl/ottlfuncs/func_remove_xml.go b/pkg/ottl/ottlfuncs/func_remove_xml.go index f499be952748..f80ebbe6f79a 100644 --- a/pkg/ottl/ottlfuncs/func_remove_xml.go +++ b/pkg/ottl/ottlfuncs/func_remove_xml.go @@ -30,17 +30,17 @@ func createRemoveXMLFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments return nil, fmt.Errorf("RemoveXML args must be of type *RemoveXMLAguments[K]") } - return removeXML(args.Target, args.XPath), nil + return removeXML(args.Target, args.XPath) } -// removeXML returns a `pcommon.String` that is a result of removing all matching nodes from the target XML. +// removeXML returns a XML formatted string that is a result of removing all matching nodes from the target XML. // This currently supports removal of elements, attributes, text values, comments, and CharData. -func removeXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K] { - return func(ctx context.Context, tCtx K) (any, error) { - if err := validateXPath(xPath); err != nil { - return nil, err - } +func removeXML[K any](target ottl.StringGetter[K], xPath string) (ottl.ExprFunc[K], error) { + if err := validateXPath(xPath); err != nil { + return nil, err + } + return func(ctx context.Context, tCtx K) (any, error) { targetVal, err := target.Get(ctx, tCtx) if err != nil { return nil, err @@ -67,7 +67,7 @@ func removeXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K }) return top.OutputXML(false), nil - } + }, nil } func validateXPath(xPath string) error { diff --git a/pkg/ottl/ottlfuncs/func_remove_xml_test.go b/pkg/ottl/ottlfuncs/func_remove_xml_test.go index 0b702c16ad3d..2dcc120a5034 100644 --- a/pkg/ottl/ottlfuncs/func_remove_xml_test.go +++ b/pkg/ottl/ottlfuncs/func_remove_xml_test.go @@ -114,14 +114,14 @@ func Test_RemoveXML(t *testing.T) { } func Test_RemoveXML_InvalidXML(t *testing.T) { - exprFunc := removeXML(invalidXMLGetter(), "/foo") - _, err := exprFunc(context.Background(), nil) + exprFunc, err := removeXML(invalidXMLGetter(), "/foo") + assert.NoError(t, err) + _, err = exprFunc(context.Background(), nil) assert.Error(t, err) } func Test_RemoveXML_InvalidXPath(t *testing.T) { - exprFunc := removeXML(invalidXPathGetter(), "!") - _, err := exprFunc(context.Background(), nil) + _, err := removeXML(invalidXPathGetter(), "!") assert.Error(t, err) }