xml 如何使用xpath检查xml中是否存在元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5689966/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if an element exists in the xml using xpath?
提问by Aravind Yarram
Below is my element hierarchy. How to check (using xpath) that AttachedXmlelement is present under CreditReportof PrimaryConsumer
下面是我的元素层次结构。如何检查(使用 xpath)在主要消费者的CreditReport下是否存在AttachedXml元素
<Consumers xmlns="http://xml.mycompany.com/XMLSchema">
<Consumer subjectIdentifier="Primary">
<DataSources>
<Credit>
<CreditReport>
<AttachedXml><![CDATA[ blah blah]]>
回答by Mads Hansen
Use the 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
boolean 函数将其参数转换为布尔值,如下所示:
一个数为真当且仅当它既不是正零也不是负零也不是 NaN
节点集为真当且仅当它不为空
字符串为真当且仅当其长度不为零
除四种基本类型之外的类型的对象以依赖于该类型的方式转换为布尔值
If there is an AttachedXmlin the CreditReportof primaryConsumer, then it will return true().
如果主Consumer的CreditReport中有一个AttachedXml,那么它将返回。true()
boolean(/mc:Consumers
/mc:Consumer[@subjectIdentifier='Primary']
//mc:CreditReport/mc:AttachedXml)
回答by Dimitre Novatchev
Use:
使用:
boolean(/*/*[@subjectIdentifier="Primary"]/*/*/*/*
[name()='AttachedXml'
and
namespace-uri()='http://xml.mycompany.com/XMLSchema'
]
)
回答by Jon Gauthier
The Saxon documentation, though a little unclear, seems to suggest that the JAXP XPath API will return falsewhen evaluating an XPath expression if no matching nodes are found.
该撒克逊文档,虽然有点不清楚,似乎表明JAXP的XPath API将返回false评估XPath表达式时,如果没有匹配的节点被发现。
This IBM articlementions a return value of nullwhen no nodes are matched.
IBM 这篇文章提到了null没有节点匹配时的返回值。
You might need to play around with the return types a bit based on this API, but the basic idea is that you just run a normal XPath and check whether the result is a node / false/ null/ etc.
您可能需要根据此 API 稍微处理一下返回类型,但基本思想是您只需运行一个普通的 XPath 并检查结果是否为节点false// null/ 等。
XPathFactory xpathFactory = XPathFactory.newInstance(NamespaceConstant.OBJECT_MODEL_SAXON);
XPath xpath = xpathFactory.newXPath();
XPathExpression expr = xpath.compile("/Consumers/Consumer/DataSources/Credit/CreditReport/AttachedXml");
Object result = expr.evaluate(doc, XPathConstants.NODE);
if ( result == null ) {
// do something
}
回答by Simon Stender Boisen
Normally when you try to select a node using xpath your xpath-engine will return null or equivalent if the node doesn't exists.
通常,当您尝试使用 xpath 选择节点时,如果该节点不存在,您的 xpath 引擎将返回 null 或等效项。
xpath: "/Consumers/Consumer/DataSources/Credit/CreditReport/AttachedXml"
If your using xsl check out this question for an answer:
如果您使用 xsl,请查看此问题以获得答案:
回答by Basheer AL-MOMANI
take look at my example
看看我的例子
<tocheading language="EN">
<subj-group>
<subject>Editors Choice</subject>
<subject>creative common</subject>
</subj-group>
</tocheading>
now how to check if creative commonis exist
现在如何检查是否creative common存在
tocheading/subj-group/subject/text() = 'creative common'
hope this help you
希望这对你有帮助
回答by x_kiter
If boolean() is not available (the tool I'm using does not) one way to achieve it is:
如果 boolean() 不可用(我使用的工具不可用),实现它的一种方法是:
//SELECT[@id='xpto']/OPTION[not(not(@selected))]
In this case, within the /OPTION, one of the options is the selected one. The "selected" does not have a value... it just exists, while the other OPTION do not have "selected". This achieves the objective.
在这种情况下,在 /OPTION 中,选项之一是选定的选项。“selected”没有值......它只是存在,而另一个 OPTION 没有“selected”。这样就达到了目的。

