Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Is there a way to extract all the elements of a xml using condition in select xpath

pooja
pooja Posts: 18

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

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    XPath 2.0 expression:
    for $item in (/testsuites/testsuite/testcase) return if ($item/failure/text()) then "Failed" else "Passed"

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    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());
        }
    }
    
  • pooja
    pooja Posts: 18

    works well with groovy scripts however in JavaScript I am facing issue to get all the values

  • jakubiak
    jakubiak Posts: 795 admin

    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());
        }
    }
    
  • pooja
    pooja Posts: 18

    thanks for your help , I did it in groovy finally

  • Poornima
    Poornima Posts: 2

    where i can write this groovy script

  • jakubiak
    jakubiak Posts: 795 admin

    You put it into an Extension Tool that is attached to the response payload output of a REST Client or Messaging Client.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited January 2022

    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.