I have a groovy code which accepts xpath expression and find the value of nodes (both XML and JSON), but I am not able to filter by attributes. Below is my code, please can I get some help?
def message = '''
<payload>
<division name="DivisionCodes">
<param name="DivisionCode">FFM-VKM</string>
<param name="DivisionGroupCode">FFM</string>
<param name="DivisionCountryCode">DE</string>
</division>
</payload>
'''
def xpathEx='*/param[@name="DivisionCountryCode"]'
def result = ''
if(xpathEx) {
def gPaths = xpathEx?.split(",") as LinkedList
def extractedData=[:]
gPaths.eachWithIndex { String entry, int idx ->
def inputData = ''
def headerKey = ''
def path = entry?.replace('/', '.')?.replace(':','')?.split('\\.') as LinkedList
if (path) {
if (message.trim().charAt(0) == '<') {
inputData = new XmlSlurper().parseText(message)
}
path.forEach({ inputData = inputData."${it}" })
}
extractedData.put(path.getLast().toString(),inputData)
}
if(extractedData.isEmpty()) {
result = ' '
} else {
result = ', XPath reference: '+extractedData?.toMapString()
}
}
println result
I am interested to get the value of param child node where attribute is "DivisionGroupCode". My xpath expression is only able to filter by child node.
What should be the correct xpathEx? OR What should correct groovy code which can take xpathEx as parameter and process on any XML payload