Forum Discussion

UNIZETO_TECHNOL_2's avatar
UNIZETO_TECHNOL_2
Occasional Contributor
14 years ago

Retrieve minoccurs (isOptional) for a field

Hi !

I have an XmlHolder for a request in a groovyScript.

I want to check, if a field in a request is optional (minoccurs>=1). How can i achieve this from an XmlHolder for the request ?
  • UNIZETO_TECHNOL_2's avatar
    UNIZETO_TECHNOL_2
    Occasional Contributor
    I have the XMLHolder for the request. I want to check the minoccurs attribute for a particular field from that request (in general, check if that field is optional)

    I don't even know how to retrieve an XMLHolder for a Schema.
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    hmm.. I'm not sure this is easily done with the public APIs, can you show the script you have so far so I can see where to pick up?

    regards!

    /ole
    eviware.com
  • UNIZETO_TECHNOL_2's avatar
    UNIZETO_TECHNOL_2
    Occasional Contributor
    From the OnRequestScript:

    XmlHolder requestHolder = new XmlHolder(mockRequest.getRequestContent());

    // now i want to get the schema information on a particular node, eg. //id[1], i want to check if that field is optional in that request

    Node n = requestHolder.getDomNode("//id[1]");

    // and what to do now ?
  • SmartBear_Suppo's avatar
    SmartBear_Suppo
    SmartBear Alumni (Retired)
    Hi!

    this requires a little more advanced scripting that uses the XmlBeans SchemaTypeSystem built internally in soapUI. First you need to create an XmlObjectTreeModel for your request message:

    // create an XmlObjectTreeModel
    def model = new com.eviware.soapui.support.editor.views.xml.outline.support.XmlObjectTreeModel(
    mockRequest.mockOperation.operation.getInterface().definitionContext.schemaTypeSystem,
    org.apache.xmlbeans.XmlObject.Factory.parse( mockRequest.requestContent) );

    To determine if an element is optional you will need to get the type information for its parent element which can give you information on the elements it contains based on its contentModel (this is XmlBeans-speak)

    // select parent of desired node so we can get its contentModel
    def nodes = model.selectTreeNodes( "//id[1]/.." );
    def schemaType = nodes[0].schemaType;

    Now we need to query the type for this information, for example to get the minOccurs of the first child particle in a sequence (which will be 0 if it is optional), we could use

    // get particle for first child in this sequence and show if it is optional
    def childParticle = schemaType.contentModel.particleChildren[0]
    log.info schemaType.contentModel.particleChildren[0].minOccurs

    Hope this helps you getting started, dig in to the XmlBeans SchemaType APIs at http://xmlbeans.apache.org/docs/2.4.0/r ... aType.html to find out how you can traverse the type information model, etc..

    regards!

    /Ole
    eviware.com
  • UNIZETO_TECHNOL_2's avatar
    UNIZETO_TECHNOL_2
    Occasional Contributor
    Thank you, this works great !

    Can You answer one more question for me:

    How can i validate a request against its schema from GroovyScript ?

    I need to get the same output as the function "validate" from the mouse right click menu in XML Source view, output like:

    line 16: Invalid decimal value: unexpected char '63'
    line 20: Invalid date value: ?

    and so on...

    All i have is the mockRequest (its XmlHolder)