Topic Options
#55449 - 11/03/17 11:00 AM Testing for existence of an XML element. Metadata?
joersmith Offline
OL Expert

Registered: 08/31/06
Posts: 163
Loc: Mount Laurel, New Jersey
I have a case where an XML file can contain two nodes under the root. I need to test for the existence of these nodes for processing.

i.e.:

<ServiceCardRequest>
<Card>
<ClientName>somedata</ClientName>
<VehicleID>somedata</VehicleID>
<LicensePlate>somedata</LicensePlate>
</Card>
<Letter>
<ari_addr1>somedata</ari_addr1>
<ari_addr2>somedata</ari_addr2>
<ari_addr3>somedata</ari_addr3>
</Letter>
</ServiceCardRequest>

Testing is required because in some cases one or the other may not be in the file being submitted.

i.e.:

<ServiceCardRequest>
<Letter>
<ari_addr1>somedata</ari_addr1>
<ari_addr2>somedata</ari_addr2>
<ari_addr3>somedata</ari_addr3>
</Letter>
</ServiceCardRequest>

Or

<ServiceCardRequest>
<Card>
<ClientName>somedata</ClientName>
<VehicleID>somedata</VehicleID>
<LicensePlate>somedata</LicensePlate>
</Card>
</ServiceCardRequest>

A simple text condition on a branch does not seem to be working reliably. I set the condition to test xmlget('/ServiceCardRequest[1]/Letter[1]',Value,KeepCase,NoTrim) not equal to blank and sometimes it works, sometimes not.

So I was wondering is anyone had a way of testing for the existence of the node and setting a variable or metadata field to use in the logical test? I am looking to test for the existence of the Card and Letter nodes.

Top
#55456 - 11/03/17 01:14 PM Re: Testing for existence of an XML element. Metadata? [Re: joersmith]
Raphael Lalonde Lefebvre Offline
OL Expert

Registered: 10/14/05
Posts: 4953
Loc: Objectif Lune Montreal
Hi joersmith,

I believe the reason why xmlget('/ServiceCardRequest[1]/Letter[1]',Value,KeepCase,NoTrim) is not consistent is because it will not differentiate between the node not existing, and the node being empty. This condition will be true if the <Letter> node doesn't exist, but also if it exists, but is empty.

Short of using scripts, maybe you could switch to a line printer emulation, and then do a text condition to look for <Letter> on the page. So if the file contains <Letter>, the condition will be true, else it will be false. Don't forget to switch the emulation back to XML after the condition!

Hope that helps!

Regards,
Raphaƫl Lalonde-Lefebvre

Top
#55458 - 11/03/17 01:51 PM Re: Testing for existence of an XML element. Metadata? [Re: joersmith]
joersmith Offline
OL Expert

Registered: 08/31/06
Posts: 163
Loc: Mount Laurel, New Jersey
Such a simple solution. It never occurred to me that I could switch back and forth between emulations.

Thank you sir!

Top