Groovy reference repeating nodes values in xml with xpath (interpolation misuse?)
<p>Setup: SOAP UI 5.2.0., Groovy step to generate XMLs.
We need to read a CSV which contains XPath-like nodes locations and new values to be placed to sample XML.<BR>
The last version of the code in the following answer fits our goal perfectly fine: <a href="https://stackoverflow.com/questions/30369986/groovy-replace-node-values-in-xml-with-xpath">Groovy replace node values in xml with xpath</a>
<BR> Only one problem:
our XML contains repeating elements and using is not possible as it misinterprets "[1]" in <code>Body.GetWeather[1].CityName</code> </p>

<pre><code>    def node = xml
key.split("\\.").each {
  node = node."${it}"
}
</code></pre>

<p>Ideally, we would also need to use something like <code>Body.GetWeather[CountryName="Africa"].CityName</code>. <BR> I tried using <code>XMLParser</code> also and experimented with syntax (see below). I am new to Groovy and I might be missing something about it. 
So, let me know if I need to approach the problem differently.</p>

<p>Below is the actual problem described in 3rd example:</p>

<pre><code>// reading XML
def myInputXML = '''
&lt;soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  &lt;soapenv:Header/&gt;
  &lt;soapenv:Body&gt;
      &lt;web:GetWeather xmlns:web="http://www.webserviceX.NET"&gt;
          &lt;web:CityName&gt;Cairo&lt;/web:CityName&gt;
          &lt;web:CountryName&gt;Africa&lt;/web:CountryName&gt;
      &lt;/web:GetWeather&gt;
      &lt;web:GetWeather xmlns:web="http://www.webserviceX.NET"&gt;
          &lt;web:CityName&gt;Heidelberg&lt;/web:CityName&gt;
          &lt;web:CountryName&gt;Germany&lt;/web:CountryName&gt;
      &lt;/web:GetWeather&gt;
      &lt;web:GetWeather xmlns:web="http://www.webserviceX.NET"&gt;
          &lt;web:CityName&gt;Strasbourg&lt;/web:CityName&gt;
          &lt;web:CountryName&gt;France&lt;/web:CountryName&gt;
      &lt;/web:GetWeather&gt;
  &lt;/soapenv:Body&gt;
&lt;/soapenv:Envelope&gt;
'''
def xml = new XmlSlurper().parseText( myInputXML )
// Example 1 //
def GetAllCities = xml.Body.GetWeather.CityName
log.info ("Example 1: "+GetAllCities.text()) // references all 3 CityName nodes, prints out - CairoHeidelbergStrasbourg
// Example 2 //
def Get2ndCity = xml.Body.GetWeather[1].CityName
log.info ("Example 2: "+Get2ndCity.text()) // references 2nd node, prints out - Heidelberg
// Example 3 //
def tmpNode1 = "Body"
def tmpNode2 = "GetWeather[0]" 
   // This problem is with interpolation of GetWeather[0]. tmpNode2 = "GetWeather" would work as Example 1
def tmpNode3 = "CityName"
def GetFirstCity = xml."${tmpNode1}"."${tmpNode2}"."${tmpNode3}"
log.info ("Example 3: "+GetFirstCity.text()) // prints "" - WHY?
log.info ("Interpolation of tmpNodes 1, 2, 3:") 
log.info ("${tmpNode1}") // prints Body
log.info ("${tmpNode2}") // prints GetWeather[0]
log.info ("${tmpNode3}") // prints CityName
</code></pre>

<p>P.S. Apologies in case if my examples are irrelevant for the actual problem, I thought they are somewhat helpful, but the goal is to improve the mentioned stackoverflow answer to support repeating elements.</p>