XPath 检查节点为空 [英] XPath Check Node is Blank

查看:42
本文介绍了XPath 检查节点为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

给定示例 XML:

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

<身体><SellResponse><SellResult><ExceptionMessage1>JourneyManager.SellJourney(segment):Segment 已经在不同的服务等级下预订</ExceptionMessage1><ExceptionMessage2 nil="true"/><NSProcessTime>0</NSProcessTime><ProcessTime>0</ProcessTime><预订 nil="true"/><成功 nil="true"/><警告 nil="true"/><错误 nil="true"/><OtherServiceInformations nil="true"/></SellResult></SellResponse></身体></信封>

TIA

解决方案

检查 ExceptionMessage1 是否为空"

如果 ExceptionMessage1 有子节点(非空),下面应该返回 true 否则返回 false :

not(//ExceptionMessage1/node())

<块引用>

检查 ExceptionMessage2 @nil == true 是否为真"

这很简单,我认为:

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

<块引用>

使用 ExceptionMessage3,我想检查它是否不为空或 nil=true"

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

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

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

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

解决方案

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

这篇关于XPath 检查节点为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆