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


回答1:


"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.




回答2:


  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



回答3:


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>    
    
    • Check if empty or nil="true"
      empty(//ExceptionMessage3/text()) or //ExceptionMessage3/@nil = "true"
      http://xpatheval.apphb.com/qp2ejO_y1

    • Check if not empty or nil="false"
      not(empty(//ExceptionMessage3/text())) or //ExceptionMessage3/@nil = "false"
      http://xpatheval.apphb.com/v1b72d6ZK

Thanks Lech Rzedzicki & har07 for the help.



来源:https://stackoverflow.com/questions/37158797/xpath-check-node-is-blank

标签

更多相关内容:请点击查看