使用XPath,如何检查节点或标记值是否为空?
给出Sample XML:
问题(需要以下XPath表达式):
1.我如何检查ExceptionMessage1是否为空白
2.我如何检查ExceptionMessage2 @nil == true?
3.如果有ExceptionMessage3,它可以是:
一个。包含异常消息
湾没有消息,@ 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>
TIA
答案 0 :(得分:3)
&#34;检查ExceptionMessage1是否为空白&#34;
如果import UIKit
class BaseViewController: UIViewController, StatePassBackDelegate {
class State {
var connected = false
var loggedIn = false
}
var state = State()
weak var delegate: StatePassBackDelegate? = nil
// MARK: Lifecycle
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
if self.isMovingFromParentViewController() == true {
delegate?.passBackState(state)
}
}
// MARK: StatePassBackDelegate functions
func passBackState(state: AnyObject) {
self.state = state as! State
}
// MARK: Helpers
final func pushStatefulViewControllerWithIdentifier(identifier: String) {
let vc = storyboard?.instantiateViewControllerWithIdentifier(identifier) as! BaseViewController
vc.state = state
vc.delegate = self
navigationController!.pushViewController(vc, animated: true)
}
}
protocol StatePassBackDelegate: class {
func passBackState(state: AnyObject)
}
有子节点(非空白),则以下内容应返回true
,否则返回ExceptionMessage1
:
false
&#34;检查ExceptionMessage2 @nil == true&#34;
我认为这很简单:
not(//ExceptionMessage1/node())
&#34;使用ExceptionMessage3,我想检查它是否为空或nil = true&#34;
以下XPath返回boolean(//ExceptionMessage2/@nil = 'true')
,如果true
不是空白或属性ExceptionMessage3
等于&#39; true&#39; :
nil
如果上下文明确需要布尔值,您可以省略boolean(//ExceptionMessage3[node() or @nil='true'])
调用。在这种情况下,转换将是自动的。
答案 1 :(得分:1)
//ExceptionMessage1/text() = ''
- 它将检查元素的内容是否为空字符串 - 只需google任何Xpath教程以获取更多示例,或者如果您需要反向。 //ExceptionMessage2/@nil = 'true'
or
组合上面的两个X路答案 2 :(得分:0)
我想有人可能会遇到类似这样的问题,所以我只是发布我从Lech&amp; amp; har07,谢谢你们。
我使用在线XPath测试程序(http://xpatheval.apphb.com)
如何检查ExceptionMessage1是空白还是空字符串
// ExceptionMessage1 / text()=''
http://xpatheval.apphb.com/KHg6afoO2
空(// ExceptionMessage1 /文本())
http://xpatheval.apphb.com/lZHxs8BO6
我如何检查ExceptionMessage2 @nil == true?
// ExceptionMessage2 / @ nil ='true'
http://xpatheval.apphb.com/8Tf7Kf8yx
boolean(// ExceptionMessage2 / @ nil ='true')
http://xpatheval.apphb.com/_KcXIH9fU
如果属性nil的值为false,则它也将返回false
如果有ExceptionMessage3怎么样,它可以是:
一个。包含异常消息
湾没有消息,@ nil =“true”(组合ExceptionMessage1&amp; ExceptionMessage2行为)
<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>
检查是否为空或nil =“true”
empty(// ExceptionMessage3 / text())或// ExceptionMessage3 / @ nil =“true”
http://xpatheval.apphb.com/qp2ejO_y1
检查是否为空或nil =“false”
not(empty(// ExceptionMessage3 / text()))或// ExceptionMessage3 / @ nil =“false”
http://xpatheval.apphb.com/v1b72d6ZK
谢谢Lech Rzedzicki&amp; har07求助。