1

Edited... My knowledge in Groovy is basic and I need to perform the following action which I have no idea how to do.

let's say I have this XML:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
 <soap:Body>
  <ns2:analyzeEffectOfReplaceOfferResponse>
      <ns2:productOfferings>
        <ns2:productOffering xsi:type="ns2:ProductOfferingInstanceCore" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
           <ns2:opExtn>C_P87531945-0993-46d1-8f23-ed23caa6e0cf</ns2:opExtn>
           <ns2:productOffering>
              <ns9:id>8447669</ns9:id>
           </ns2:productOffering>
           <ns2:productOffering>
              <ns9:id>8447100</ns9:id>
           </ns2:productOffering>
        </ns2:productOffering>
     </ns2:productOfferings>
  </ns2:analyzeEffectOfReplaceOfferResponse>
</soap:Body>
</soap:Envelope>

given an XPath expression like:

/Body/analyzeEffectOfReplaceOfferResponse/productOfferings/productOffering/productOffering

and GPathResult as:

gpathRequest = new XmlSlurper().parse(inputFile)

I know how to use findAll to search for all related tags. Let's say I stored the Xpath in a xPath variable:

def gpath = "XML." + xPaths
child = Eval.me( 'XML', gpathRequest, gpath ).depthFirst().findAll{it.value}

However I am not sure how to handle the result here in form of collection. How can I add a new property after finding the result I need?

Yossi Even
  • 45
  • 8
  • 1
    Can you come up with an example that makes sense? – tim_yates Feb 26 '18 at 16:20
  • Sorry, I am having problem phrasing what I need. Updated the question on what I reached so far. However I am not sure even this can have any Xpath expression.. I tried and it failed with something like: "//*[@attr=="id"]. So my question is actually twofold - 1. can I use any Xpath expression. 2. how do use the result to add new property to productOffering node. – Yossi Even Feb 26 '18 at 16:48
  • @YossiEven, can you please check the below solution to see if that helps? – Rao Feb 26 '18 at 17:31
  • I doubt you can navigate a GPathResult with an XPath query? – cfrick Feb 26 '18 at 19:21

1 Answers1

1

You can create a list of new product offerings and loop thru the list to add new productOffering nodes.

Do not need to use xpath. Instead find parent node which productOfferings and appendNode be used to achieve the same.

See the below script:

def newOfferings = [8447101, 8447102]

def xml = new XmlSlurper().parse(inputFile)

def offerings = xml.'**'.find{it.name() == 'productOfferings'}

newOfferings.each { val ->
   offerings.appendNode {
       'ns2.productOffering' {
          'ns9.id'(val)
       }
   }
}

You can quick try the same online demo

Rao
  • 20,781
  • 11
  • 57
  • 77
  • Thing is the Xpath is given by outside source (configuration) so I don't know what is the actual path I am looking for. This is why I am using it as input on the eval. doing it like you did means I know how the XML And the Path will look like, which I cannot assume to know. I want to be able to dynamically extract the nodes I need and add my attribute – Yossi Even Feb 26 '18 at 18:36