Mark O'Reilly · Apr 30, 2020

How to work with Undefined value from Correlated XML Message


Sorry I can't find the correct documentation for this. It's very simple I think. 

I open up an XML reader class and correlate the XML to a message type. 

My XML will read something like 


This gets correlated into a message with the following property 

Property StatusLastChecked As %String(XMLPROJECTION = "element");

I can use the following to get the other properties out 

i.e. docs.RelayedDocuments.GetAt(i).DateAdded will get the date added properties

When i try for the one with the blank value for the blank entry with a trace I get undefined traced out 



I just want a line to basically try say if it has no value then do something. I tried the following but couldn't do anything based on if it was blank

set LastChecked= ""
set LastChecked= docs.RelayedDocuments.GetAt(i).StatusLastChecked
if LastChecked="undefined" {$$$TRACE("Gotcha")}

if LastChecked="" {$$$TRACE("Gotcha")}



0 318
Discussion (6)3
Log in or sign up to continue

You can add this to your code

$$$TRACE("LastChecked value is: '" _ LastChecked _ "'")

to get LastChecked value.

Hi Eduard. Sorry my bad for not including that in my original snippet my full snippet is 

#dim docs As Penn.EDT.XML.RelayedDocument
// docs now contains our results. Create a EDT document message for each
for i=1:1:docs.RelayedDocuments.Count() {
set LastChecked= ""
set LastChecked= docs.RelayedDocuments.GetAt(i).StatusLastChecked
if LastChecked="undefined" {$$$TRACE("Gotcha")}

The LastChecked value trace prints out "undefined" as per the screenshot but if i say if Lastchecked="undefined" it doesn't work. Should see the trace Gotcha before it prints out the last checked date 


$$$TRACE("LastChecked value is: '" _ LastChecked _ "', compare: " _ (LastChecked="undefined"))

It's still not right. Might just use $length as can't figure it out

set LastChecked= docs.RelayedDocuments.GetAt(1).StatusLastChecked
$$$TRACE("Date Added "_docs.RelayedDocuments.GetAt(1).DateAdded)
$$$TRACE("Date Last Checked "_docs.RelayedDocuments.GetAt(1).StatusLastChecked)
$$$TRACE("LastChecked value is: '" _ LastChecked _ "', compare: " _ (LastChecked="undefined"))
if LastChecked= "" {$$$TRACE("blank")
}else{$$$TRACE("not blank " _$LENGTH(LastChecked) )}
if $L(LastChecked)<2 {$$$TRACE("Blank")}

might just use the length as at least it does work (length when blank was 1) 

Hi Mark.

I don't know where "undefined" word comes from on the trace, but when %XML.Reader reads empty element it reads it as character with code 0 -- $char(0), that corresponds to empty string in SQL:

Class Community.Test Extends (%RegisteredObject, %XML.Adaptor)

Property StatusLastChecked As %String(XMLPROJECTION = "element");

ClassMethod test()
        set xml = "<root><Test><StatusLastChecked></StatusLastChecked></Test></root>"
        set reader = ##class(%XML.Reader).%New()
        do reader.OpenString(xml)
        do reader.Correlate("Test","Community.Test")
        do reader.Next(.obj)
        zw obj


USER>do ##class(Community.Test).test()
obj=3@Community.Test  ; <OREF>
+----------------- general information ---------------
|      oref value: 3
|      class name: Community.Test
| reference count: 2
+----------------- attribute values ------------------
|  StatusLastChecked = $c(0)

So in your code you can compare with $C(0).

Also for details on handling empty strings, see following section in documentation:

Handling Empty Strings and Null Values

Thanks this looks great and when i try this again with code from scratch i will use this. 

Ended up using the Length method as above but this sounds correct to know in the future which won't be long :)