user2345148
user2345148

Reputation: 11

In XML PARSE using COBOL the FUNCTION LENGTH (XML-TEXT) calculating 1 for XML-TEXT = blanks

I am trying to parse an XML document in COBOL using XML PARSE, one segment at a time. In the CONTENT-CHARACTERS check I am doing COMPUTE WS-XML-LEN = FUNCTION LENGTH (XML-TEXT).

There are some START OF ELEMENT for which I do not want to process, for them also it is going into the CONTENT-CHARACTERS check and calculating the length of (XML-TEXT) = 1 even though the XML-TEXT is blank.

Could you let me know what could be the reason and I should modify in my program.

Upvotes: 0

Views: 2052

Answers (1)

Bill Woodger
Bill Woodger

Reputation: 13076

FUNCTION LENGTH (xyz) gives you the length OF THE FIELD, not the "length" of the DATA. COBOL has no "string delimiter" (except for a special use for a literal).

If xyz is PIC X(30) it will return 30 "every time", irrespective of the content of the field.

You haven't mentioned which compiler or operating system you are using, but if you are using an IBM Cobol, you should produce the "LIST" output (the generated pseudo-assembler code). You will see that the code generated for FUNCTION LENGTH ( something ) is just an MVC from the "literal pool" which will be pointing at a constant value which is the same as the length of your field, and which is (likely) only there because you are using FUNCTION LENGTH.

The LENGTH OF "special register" is similar, if you have it.

If you want to know if a field is all blank:

IF something EQUAL TO SPACES

If you need to know the length of a field given that you know a specific "trailing" or "leading" value (spaces, zero, low-values, whatever) then you have to write some code to do it.

For counting data and ignoring trailing spaces, a common way us to use FUNCTION REVERSE and then INSPECT ... TALLYING ... FOR LEADING SPACE. There are other ways.

You'll need the length of the field (to calculate the length of the data) but the length of the field is invariant so only calculate (FUNCTION LENGTH or plain LENGTH OF) once.

There is a subtle difference between FUNCTION LENGTH and LENGTH OF, which probably won't affect your task.

Upvotes: 1

Related Questions