ASG
IBM
Zystems
Cressida
Icon
Netflexity
 
  MQSeries.net
Search  Search       Tech Exchange      Education      Certifications      Library      Info Center      SupportPacs      LinkedIn  Search  Search                                                                   FAQ  FAQ   Usergroups  Usergroups
 
Register  ::  Log in Log in to check your private messages
 
RSS Feed - WebSphere MQ Support RSS Feed - Message Broker Support

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » check for existence of an element

Post new topic  Reply to topic
 check for existence of an element « View previous topic :: View next topic » 
Author Message
chris boehnke
PostPosted: Thu May 28, 2015 11:20 am    Post subject: check for existence of an element Reply with quote

Partisan

Joined: 25 Jul 2006
Posts: 369

Hi All,
How do we check for existence of XML tag in a incoming XML element.
Below is the sample xml. I want to check if "Test2" tag exists or not.

<Test1>
<Test2>
<value>556</value>
<description>hfsdkh</description>
</Test2>
</Test1>
Back to top
View user's profile Send private message
Vitor
PostPosted: Thu May 28, 2015 11:22 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

Do you mean you want to test to see if you have:

Code:

<Test1>
 <Test2>
 <value>556</value>
 <description>hfsdkh</description>
 </Test2>
</Test1>


or this:

Code:

<Test1>
 <value>556</value>
 <description>hfsdkh</description> 
</Test1>

_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
chris boehnke
PostPosted: Thu May 28, 2015 11:26 am    Post subject: Reply with quote

Partisan

Joined: 25 Jul 2006
Posts: 369

I want to check if I have "Test2" xml element
Back to top
View user's profile Send private message
Vitor
PostPosted: Thu May 28, 2015 11:31 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

chris boehnke wrote:
I want to check if I have "Test2" xml element


But why?

If you want know if you have it, you can test directly; if you want to know if you have it because you want to access the value & description elements you can access them anonymously.

In XML terms, are you interested in the existence or not of the complex element Test2, or interested in the child elements?

It only matters because it changes the code you need to do it.
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
Vitor
PostPosted: Thu May 28, 2015 11:32 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

Consider:

Code:

<Test1>
 <Test2>
 <value>556</value>
 <description>hfsdkh</description>
 </Test2>
<Test3>
 <value>557</value>
 <description>asadsd</description>
 </Test3>
</Test1>


Still interested in the existence of Test2? Or the number of descriptions you have?
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
chris boehnke
PostPosted: Thu May 28, 2015 11:44 am    Post subject: Reply with quote

Partisan

Joined: 25 Jul 2006
Posts: 369

I still need to test the existence of XML element to do the transformation.
Back to top
View user's profile Send private message
Vitor
PostPosted: Thu May 28, 2015 11:55 am    Post subject: Reply with quote

Grand High Poobah

Joined: 11 Nov 2005
Posts: 26093
Location: Texas, USA

chris boehnke wrote:
I still need to test the existence of XML element to do the transformation.


Then you need something like:

Code:

IF InputRoot.XMLNSC.Test1.Test2 IS NOT NULL THEN


or

Code:

MOVE someReference TO InputRoot.XMLNSC.Test1.Test2;
IF LASTMOVE(someReference) THEN


or

Code:

MOVE someReference TO InputRoot.XMLNSC.Test1;
MOVE someReference FIRSTCHILD;
IF FIELDNAME(someReference) = 'Test2' THEN


or

...
_________________
Honesty is the best policy.
Insanity is the best defence.
Back to top
View user's profile Send private message
fjb_saper
PostPosted: Thu May 28, 2015 12:05 pm    Post subject: Reply with quote

Grand High Poobah

Joined: 18 Nov 2003
Posts: 20756
Location: LI,NY

And think about the odd case
Code:
<test1>
   <test2 xsi:nil="true"/>
</test1>


Have fun
_________________
MQ & Broker admin
Back to top
View user's profile Send private message Send e-mail
chris boehnke
PostPosted: Fri May 29, 2015 7:58 am    Post subject: Reply with quote

Partisan

Joined: 25 Jul 2006
Posts: 369

Thanks Vitor and fjb!!

Code:
IF InputRoot.XMLNSC.Test1.Test2 IS NOT NULL THEN


Doesnt work as expected. This statement checks for the value of Test2.

I tried the below one and it works.
Code:

MOVE someReference TO InputRoot.XMLNSC.Test1.Test2;
IF LASTMOVE(someReference) THEN


Thanks!!
Back to top
View user's profile Send private message
inMo
PostPosted: Fri May 29, 2015 9:25 am    Post subject: Reply with quote

Master

Joined: 27 Jun 2009
Posts: 216
Location: NY

To be sure, your solution will check for the presence of Test2. If Test2 is present, but empty, your test will still prove true. As long as your intention is to check for the absence of the element Test2 your code is fine. If however your requirement is such that there is no difference between the absence of Test2 and the presence of an empty Test2, you will need to do something different.
Back to top
View user's profile Send private message
mqjeff
PostPosted: Fri May 29, 2015 9:35 am    Post subject: Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 17447

I think I have had some success testing for a missing element by testing if the FIELDNAME is null.
Back to top
View user's profile Send private message
rekarm01
PostPosted: Sat May 30, 2015 1:47 am    Post subject: Re: check for existence of an element Reply with quote

Grand Master

Joined: 25 Jun 2008
Posts: 1415

chris boehnke wrote:
How do we check for existence of XML tag in a incoming XML element.

Aside from some of the suggestions already given, there is also the EXISTS() function:

Code:
IF EXISTS(InputRoot.XMLNSC.Test1.Test2[]) THEN ...
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic  Reply to topic Page 1 of 1

MQSeries.net Forum Index » WebSphere Message Broker (ACE) Support » check for existence of an element
Jump to:  



You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Protected by Anti-Spam ACP
 
 


Theme by Dustin Baccetti
Powered by phpBB © 2001, 2002 phpBB Group

Copyright © MQSeries.net. All rights reserved.