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.

scripting remove empty elements

jngojngo
jngojngo Posts: 46

I need to removed the empty elements using script.

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭

    Are you asking how to remove empty XML elements from an XML document? You could probably write an XSL file to do this then use SOAtest's XSLT tool.

  • jngojngo
    jngojngo Posts: 46

    I should have explain this further. I have got two elements on the request. Element 1 is mandatory and Element2 is optional field and dependent on the value of Element1. Let say Element 2 becomes required. Since the datasource value of Element2 is blank. It requires to generate a value. So I wrote the script when it's mandatory(generate a value when the datasource is blank). However, since I am utilizing the script to generate the value. How do I removed Element2 when it's optional.

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited November 2018

    How do I removed Element2 when it's optional.

    If you are using the Form Input view, there is an option to "Exclude Element with Empty String". This requires you to be using a data source where an empty value in a particular column can be used to control whether an element is excluded or not. See Form Input View Options.

    Otherwise, you can always chain an Extension Tool or XSLT Tool to the outgoing request output to removing empty elements. Such tools can be used that way to alter or post-process the outgoing message before it is sent. Such tools receive the original message as input and return the altered message as output. Unfortunately, I don't have sample code or XSL at the moment to share.

  • jngojngo
    jngojngo Posts: 46

    Does this also apply to form_JSON

  • benken_parasoft
    benken_parasoft Posts: 1,228 ✭✭✭
    edited November 2018

    Does this also apply to form_JSON

    Yes. The Form JSON view has options that are analogous to the Form Input view. In general, things you can do in Form Input for XML work similarly for JSON in the Form JSON view. If you right-click on a JSON property then you should see an "Exclude with Empty String" option, similar to right clicking on an XML element in the Form Input view. If your test is using a service definition (WSDL, OpenAPI, Swagger, etc.) then this works as long as the service definition defines the element or property as being optional (the views won't let you exclude something that's required).

    Are you using JSON or XML? This is why I had asked about whether you were referring to XML elements or not. What I mentioned about using XSL only applies to XML documents. However, if you wanted to use XSL, then you can use the XML Converter tool as needed to convert JSON to XML and back.

  • jngojngo
    jngojngo Posts: 46

    I am using both SOAP and JSON message format. But, for this particular example it is for REST. Thanks.