Borgy Manotoy
Borgy Manotoy

Reputation: 2038

XPath Check Node is Blank

Using XPath, how to check if the node or tag value is blank?

Given the Sample XML:

Questions (Need XPath expression for the following):
1. how will I check if ExceptionMessage1 is blank.
2. how will I check if ExceptionMessage2 @nil == true?
3. how about if there will be ExceptionMessage3, it can either be:
a. Contains exception message
b. No message, with @nil="true" (Combined ExceptionMessage1 & ExceptionMessage2 behaviors)

<Envelope>
   <Body>
      <SellResponse>
         <SellResult>
            <ExceptionMessage1>JourneyManager.SellJourney(segment): Segment already booked under different class of service</ExceptionMessage1>
            <ExceptionMessage2 nil="true"/>
            <NSProcessTime>0</NSProcessTime>
            <ProcessTime>0</ProcessTime>
            <Booking nil="true"/>
            <Success nil="true"/>
            <Warning nil="true"/>
            <Error nil="true"/>
            <OtherServiceInformations nil="true"/>
         </SellResult>
      </SellResponse>
   </Body>
</Envelope>

TIA

Upvotes: 3

Views: 19096

Answers (3)

Borgy Manotoy
Borgy Manotoy

Reputation: 2038

I guess somebody might encounter similar issues like this, so I just post the solutions I gather from Lech & har07, thanks guys.

I use the online XPath Tester (http://xpatheval.apphb.com)

  1. how will I check if ExceptionMessage1 is blank or empty string.
    //ExceptionMessage1/text() = ''
    http://xpatheval.apphb.com/KHg6afoO2

    empty(//ExceptionMessage1/text())
    http://xpatheval.apphb.com/lZHxs8BO6

  2. how will I check if ExceptionMessage2 @nil == true?
    //ExceptionMessage2/@nil = 'true'
    http://xpatheval.apphb.com/8Tf7Kf8yx

    boolean(//ExceptionMessage2/@nil = 'true')
    http://xpatheval.apphb.com/_KcXIH9fU

    If the attribute nil's value is false, it will also return false

  3. how about if there will be ExceptionMessage3, it can either be:
    a. Contains exception message
    b. No message, with @nil="true" (Combined ExceptionMessage1 & ExceptionMessage2 behaviors)

    <Envelope>    
       <Body>    
           <SellResponse>    
               <SellResult>    
                   <ExceptionMessage1>JourneyManager.SellJourney(segment): Segment already booked under different class of service</ExceptionMessage1>    
                   <ExceptionMessage2 nil="true"/>    
                   <ExceptionMessage3 nil="false">JourneyManager.SellJourney(segment): No funds to proceed with the transaction</ExceptionMessage3>    
                   <NSProcessTime>0</NSProcessTime>    
                   <ProcessTime>0</ProcessTime>    
                   <Booking nil="true"/>    
                   <Success nil="true"/>    
                   <Warning nil="true"/>    
                   <Error nil="true"/>    
                   <OtherServiceInformations nil="true"/>    
               </SellResult>    
           </SellResponse>    
       </Body>    
    </Envelope>    
    

Thanks Lech Rzedzicki & har07 for the help.

Upvotes: 2

har07
har07

Reputation: 89305

"check if ExceptionMessage1 is blank"

The following should return true if ExceptionMessage1 has child node (not blank) and return false otherwise :

not(//ExceptionMessage1/node())

"check if ExceptionMessage2 @nil == true"

This is straightforward, I think :

boolean(//ExceptionMessage2/@nil = 'true')

"With ExceptionMessage3, I want to check if it is not blank or nil=true"

The following XPath returns true, if there is ExceptionMessage3 that is not blank or has attribute nil equals 'true' :

boolean(//ExceptionMessage3[node() or @nil='true'])

You might be able to omit boolean() call if the context explicitly expects a boolean value. The conversion will be automatic in that case.

Upvotes: 6

Lech Rzedzicki
Lech Rzedzicki

Reputation: 435

  1. //ExceptionMessage1/text() = '' - it will check if the content of the element is an empty string - just google any Xpath tutorial for more examples or if you need the inverse.
  2. //ExceptionMessage2/@nil = 'true'
  3. Just use or to combine the two Xpaths above

Upvotes: 1

Related Questions