Skip to content

Commit 2c88acc

Browse files
djaglowskizeck-ops
authored andcommitted
[pkg/ottl] Add support for non-element nodes in GetXML (open-telemetry#36821)
When originally implemented, only element nodes were selectable, but there doesn't seem to be any reason why we shouldn't be able to select these other types as well.
1 parent 8acf0cb commit 2c88acc

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

.chloggen/get-xml-nonelements.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: GetXML Converter now supports selecting text, CDATA, and attribute (value) content.
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36821]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: []

pkg/ottl/ottlfuncs/README.md

+12
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,18 @@ Get all elements in the document with tag "a" that have an attribute "b" with va
835835

836836
- `GetXML(body, "//a[@b='c']")`
837837

838+
Get `foo` from `<a>foo</a>`
839+
840+
- `GetXML(body, "/a/text()")`
841+
842+
Get `hello` from `<a><![CDATA[hello]]></a>`
843+
844+
- `GetXML(body, "/a/text()")`
845+
846+
Get `bar` from `<a foo="bar"/>`
847+
848+
- `GetXML(body, "/a/@foo")`
849+
838850
### Hex
839851

840852
`Hex(value)`

pkg/ottl/ottlfuncs/func_get_xml.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ func getXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K] {
5252

5353
result := &xmlquery.Node{Type: xmlquery.DocumentNode}
5454
for _, n := range nodes {
55-
if n.Type != xmlquery.ElementNode {
55+
switch n.Type {
56+
case xmlquery.ElementNode, xmlquery.TextNode:
57+
xmlquery.AddChild(result, n)
58+
case xmlquery.AttributeNode, xmlquery.CharDataNode:
59+
// get the value
60+
xmlquery.AddChild(result, &xmlquery.Node{
61+
Type: xmlquery.TextNode,
62+
Data: n.InnerText(),
63+
})
64+
default:
5665
continue
5766
}
58-
xmlquery.AddChild(result, n)
5967
}
6068
return result.OutputXML(false), nil
6169
}

pkg/ottl/ottlfuncs/func_get_xml_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,22 @@ func Test_GetXML(t *testing.T) {
7474
want: `<a></a>`,
7575
},
7676
{
77-
name: "ignore attribute selection",
77+
name: "get attribute selection",
7878
document: `<a foo="bar"></a>`,
79-
xPath: "/@foo",
80-
want: ``,
79+
xPath: "/a/@foo",
80+
want: `bar`,
8181
},
8282
{
83-
name: "ignore text selection",
83+
name: "get text selection",
8484
document: `<a>hello</a>`,
8585
xPath: "/a/text()",
86-
want: ``,
86+
want: `hello`,
8787
},
8888
{
89-
name: "ignore chardata selection",
89+
name: "get chardata selection",
9090
document: `<a><![CDATA[hello]]></a>`,
9191
xPath: "/a/text()",
92-
want: ``,
92+
want: `hello`,
9393
},
9494
}
9595
for _, tt := range tests {

0 commit comments

Comments
 (0)