如何使用 XPath 检查 XML 中是否存在元素? [英] How to check if an element exists in the XML using XPath?

查看:36
本文介绍了如何使用 XPath 检查 XML 中是否存在元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的元素层次结构.如何检查(使用 XPath)AttachedXml 元素是否存在于 Primary Consumer

Below is my element hierarchy. How to check (using XPath) that AttachedXml element is present under CreditReport of Primary Consumer

<Consumers xmlns="http://xml.mycompany.com/XMLSchema">
       <Consumer subjectIdentifier="Primary">
          <DataSources>
               <Credit>
                   <CreditReport>
                      <AttachedXml><![CDATA[ blah blah]]>

推荐答案

使用 boolean() XPath 函数

Use the boolean() XPath function

布尔函数将其转换为布尔值的参数如下:

The boolean function converts its argument to a boolean as follows:

  • 一个数为真当且仅当它既不是正面也不是负面零或 NaN

  • a number is true if and only if it is neither positive or negative zero nor NaN

节点集为真当且仅当它是非空的

a node-set is true if and only if it is non-empty

一个字符串为真当且仅当它的长度非零

a string is true if and only if its length is non-zero

一个非类型的对象四种基本类型转换为以一种依赖于的方式布尔值那种类型

an object of a type other than the four basic types is converted to a boolean in a way that is dependent on that type

如果primary ConsumerCreditReport中有AttachedXml,那么它会返回true().

If there is an AttachedXml in the CreditReport of primary Consumer, then it will return true().

boolean(/mc:Consumers
          /mc:Consumer[@subjectIdentifier='Primary']
            //mc:CreditReport/mc:AttachedXml)

这篇关于如何使用 XPath 检查 XML 中是否存在元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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