Is there a way to extract all the elements of a xml using condition in select xpath
I have an xml as input and I retrieve the values in xml databank
But I am not able to retrieve it if for all elements at one go
For Fist element I get Passed
if (/testsuites/testsuite[1]/testcase[1]/failure[1]/text() ) then "Failed" else "Passed"
but If kepts as
if (/testsuites/testsuite[1]/testcase/failure/text() ) then "Failed" else "Passed"
still I get only one child node output and not for all as
Failed
Passed
Passed
Failed
etc
Comments
-
XPath 2.0 expression:
for $item in (/testsuites/testsuite/testcase) return if ($item/failure/text()) then "Failed" else "Passed"0 -
Example Groovy script:
import javax.xml.transform.stream.*; import net.sf.saxon.s9api.*; import com.parasoft.api.*; void test(Object input) { Processor processor = new Processor(false); DocumentBuilder docBuilder = processor.newDocumentBuilder(); XdmNode node = docBuilder.build(new StreamSource(new StringReader(input))); XPathCompiler compiler = processor.newXPathCompiler(); XPathExecutable compiledXPath = compiler.compile( 'for $item in (/testsuites/testsuite/testcase) return if ($item/failure/text()) then \"fail\" else \"passed\"'); XPathSelector selector = compiledXPath.load(); selector.setContextItem(node); for (XdmItem item in selector.evaluate()) { Application.showMessage(item.toString()); } }
1 -
works well with groovy scripts however in JavaScript I am facing issue to get all the values
0 -
Here's a script that you can use in the "JavaScript (Oracle Nashorn)" engine:
var Processor = Java.type("net.sf.saxon.s9api.Processor"); var StreamSource = Java.type("javax.xml.transform.stream.StreamSource"); var StringReader = Java.type("java.io.StringReader"); var Application = Java.type("com.parasoft.api.Application"); function test(input) { processor = new Processor(false); docBuilder = processor.newDocumentBuilder(); node = docBuilder.build(new StreamSource(new StringReader(input))); compiler = processor.newXPathCompiler(); compiledXPath = compiler.compile( 'for $item in (/testsuites/testsuite/testcase) return if ($item/failure/text()) then \"fail\" else \"passed\"'); selector = compiledXPath.load(); selector.setContextItem(node); iterator = selector.evaluate().iterator(); while (iterator.hasNext()) { Application.showMessage(iterator.next()); } }
1 -
thanks for your help , I did it in groovy finally
0 -
where i can write this groovy script
0 -
You put it into an Extension Tool that is attached to the response payload output of a REST Client or Messaging Client.
0 -
If you are using a recent release of SOAtest then you do not need the script. Just run the XPath expression I described directly in your XML Data Bank or XML Transformer tool. The script was a work around for an earlier limitation with some XML tooling not supporting XPath expressions that returned non-node values like a custom string or a list of strings.
0