XPath 检查节点为空

Bor*_*toy 3 xml xpath

使用XPath,如何检查节点或标签值是否为空?

鉴于示例 XML:

问题(以下需要 XPath 表达式):
1. 我将如何检查 ExceptionMessage1 是否为空。
2. 我将如何检查 ExceptionMessage2 @nil == true?
3.怎么样,如果会有ExceptionMessage3,它可以是:
一。包含异常消息
b. 没有消息,@nil="true"(组合 ExceptionMessage1 和 ExceptionMessage2 行为)

<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>
Run Code Online (Sandbox Code Playgroud)

TIA

har*_*r07 5

“检查 ExceptionMessage1 是否为空”

true如果ExceptionMessage1有子节点(非空白),则应返回以下内容,false否则返回:

not(//ExceptionMessage1/node())
Run Code Online (Sandbox Code Playgroud)

“检查是否 ExceptionMessage2 @nil == true”

这很简单,我认为:

boolean(//ExceptionMessage2/@nil = 'true')
Run Code Online (Sandbox Code Playgroud)

“使用 ExceptionMessage3,我想检查它是否不是空白或 nil=true”

以下 XPath 返回true,如果有ExceptionMessage3不为空或属性nil等于 'true' :

boolean(//ExceptionMessage3[node() or @nil='true'])
Run Code Online (Sandbox Code Playgroud)

boolean()如果上下文明确需要布尔值,您可能可以省略调用。在这种情况下,转换将是自动的。