Skip to Content
avatar image
Former Member

exists builtin function does not return false for missing node

Hi,

With all due respect to the posts against this issue in a previous forum message

http://forums.sdn.sap.com/thread.jspa?threadID=1903601

unfortunatley I think that you missed the point. When the node that is passed to the "exists" function is not present in the input structure the "exists" function should return false. In fact the "exists" function should always return either true or false and should NEVER return "null".

If however the node passed to the "exists" function is a missing child of a missing node, then the input to the function is "null" and the output of the function is "null". This must be classified as a bug, and not as "working as expected"; if you accept the premise that the documentation for the "exists" function (that it should return either "true" of "false") is correct.

excerpt from the PI 711 documentation of the "exists" function

Use

Use this function to determine whether a particular source field exists in the XML instance to be processed. If it does, exists() returns the value true, otherwise it returns the value false.

Stephen

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Feb 08, 2012 at 07:09 AM

    Hi Stephen,

    How about using "exists(node) and exists(parent node)"? This way you will get "false" even if the node does not exist simply because of the parent node not being there?

    Also, depending on the actual requirement, you might want to use mapWithDefault, as mentioned in the thread you gave us the link to.

    Hope this helps,

    Greg

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member

      Hi Greg,

      Thanks for your reply.

      There are many ways to work around this bug, probably the easiest of which is to write a myExists() UDf (code below) that works properly (which I have done). This eliminates the necessity to clutter up the map with a series of other builtin functions. I really just wanted to make the point that the buitlin exists() function does NOT work as documented, and that everyone in the previous post, including Stefan (which really surprises me) missed the point. I am also surprised that no one has raised an OSS with SAP on this; although perhaps I should not be as I most likely will not get around to it either 😊

      public void myExists(String[] var1, ResultList result, Container container) throws StreamTransformationException{

      /*

      • Itu2019s a context function otherwise it would not be called if there is no value.

      • it should only be called when there can be only one instance of the node in the context

      • otherwise it does not make sense. it returns true if the node exists and false if it does not.

      */

      if (var1.length == 0) result.addValue("false");

      else result.addValue("true");

      return;

      }

      You can use this any place you would normally use the exists() function.

      Stephen

      Edited by: Stephen Omond on Feb 9, 2012 4:35 AM