java - 如何使用 XPath 检查 XML 中是否存在元素?

标签 java xml xpath saxon

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

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

最佳答案

使用 boolean() XPath function

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

  • 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

如果primaryConsumerCreditReport中有AttachedXml,则返回真()

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

关于java - 如何使用 XPath 检查 XML 中是否存在元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5689966/

相关文章:

java - 在 Eclipse Android 中导入 JAR (JAudioTagger)

xml - 如何使用 Express.js 和 Backbone.js 将 XML Web 服务输出转换为 Json

python - Beautiful Soup 的 XML 数据不完整

python-3.x - 找到WebDriver元素后,如何在该元素上应用xpath来进一步缩小其范围?

java - 使用 eclipse 抑制整个类未使用的警告

java - 当 IdempotReceiverInterceptor 丢弃消息时流程挂起(在第 4 条消息之后)

java - Spark GraphX 中完整图的分区策略

使用 scanner.hasNext() 时出现 Java NullPointerException;

java - 如何使用 Java 和 SAX 解析带有偶尔 XML 标记的纯文本文件?

python - 选择并修改特定文本后的 xpath 节点