Given an XML document such as this:
<Processes>
<Params>
<ParamName> today </ParamName>
<ParamType>1</ParamType>
<ParamValue/>
</Params>
<Params>
<ParamName> today </ParamName>
<ParamType>2</ParamType>
<ParamValue/>
</Params>
<Params>
<ParamName> today </ParamName>
<ParamType>3</ParamType>
<ParamValue/>
</Params>
<Processes>
Using the beevik/etree for Go, how can I access <ParamValue/>
for each instance of <Params>
in the document to populate it with a value when <ParamName>
has a particular, specific value.
In the given example, I’d want to populate all <ParamValue/>
nodes in all the <Params>
nodes with 05/02/2024 when <ParamName>
holds the value today.
This code only works on the first instance of the <Params>
node in a document containing many instances where <ParamName>
== today , although the loop should seemingly access every instance of <Params>
:
for _, elem1 := range doc.FindElements(".//Processes//Params//ParamName") {
if elem1 == nil {
log.Fatal("Check XPath)
}
s := elem1.Text()
if s == "today" {
elems2 := elem1.FindElement("//ParamValue")
elems2.SetText("05/02/2024")
}
}
How can I do this? Why doesn’t range doc.FindElements(".//Processes//Params//ParamName")
find every instance of <Params>
?