xml 如果元素存在且非空,如何使用 XPath 判断?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15909348/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:58:10  来源:igfitidea点击:

How to tell using XPath if an element is present and non empty?

xmlxpathmule

提问by Charu Khurana

I have an input XML something on this line:

我在这一行有一个输入 XML:

<Holding id="12">
    <Policy>
        <HoldingForm tc="1">Individual</HoldingForm>
        <PolNumber>848433</PolNumber>
        <LineOfBusiness tc="1">Life</LineOfBusiness>
        <CarrierCode>67644</CarrierCode>
    </Policy>
</Holding>

My manipulation on this XML depends on if <PolNumber>(its an optional element in schema) has a value or not. I'm using Mule 3.3 xpathevaluator to do this and my XPath expression looks this:

我对这个 XML 的操作取决于<PolNumber>(它是模式中的一个可选元素)是否有值。我正在使用 Mule 3.3xpath评估器来执行此操作,我的 XPath 表达式如下所示:

<expression-filter expression="#[xpath('//acord:Holding/acord:Policy/acord:PolNumber').text != empty]"/> 

This works fine as long as <PolNumber>element is present or <PolNumber/>is empty element. But if <PolNumber>is absent, above expression throws exception.

只要<PolNumber>元素存在或<PolNumber/>为空元素,就可以正常工作。但是如果<PolNumber>不存在,上面的表达式会抛出异常。

I tried using XPath booleanfunction but it returns truefor <PolNumber/>. Is there a better way of checking if an element is present and non-empty?

我试着用XPath的布尔函数,但返回true<PolNumber/>。有没有更好的方法来检查元素是否存在且非空?

EDIT:

编辑:

This is the configuration of namespace manager in my mule config

这是我的 mule 配置中命名空间管理器的配置

<xm:namespace-manager includeConfigNamespaces="true">
    <xm:namespace prefix="acord" uri="http://ACORD.org/Standards/Life/2" />
    <xm:namespace prefix="soap" uri="http://schemas.xmlsoap.org/soap/encoding/" />
</xm:namespace-manager>

回答by Dimitre Novatchev

Use:

使用

boolean(//acord:Holding/acord:Policy/acord:PolNumber/text()[1])

this produces true()if //acord:Holding/acord:Policy/acord:PolNumberhas a first text-node child, and false()otherwise.

这将产生true(),如果//acord:Holding/acord:Policy/acord:PolNumber有第一个文本节点孩子,和false()其他。

Do note: This is more efficient than counting all text-node children just to compare the count with 0.

请注意:这比仅将计数与 0 进行比较而计算所有文本节点子节点更有效。

回答by Jens Erat

You can use boolean(...)for checking if it's empty, but make sure to look insidethe element.

您可以boolean(...)用于检查它是否为空,但请确保查看元素内部

boolean(//PolNumber/node())

This also works if other nodes are contained. If you want to limit to text nodes, replace node()by text(). You could want to use //text()instead, then the query will also yield true for text nodes inside other child elements of <PolNumber/>.

如果包含其他节点,这也适用。如果要限制为文本节点,请替换node()text(). 您可能想//text()改用,然后查询也将为<PolNumber/>.

回答by user3190499

How about expression="#[?xpath('//acord:Holding/acord:Policy/acord:PolNumber').text != empty]"? This should work in in all situations

怎么样expression="#[?xpath('//acord:Holding/acord:Policy/acord:PolNumber').text != empty]"?这应该适用于所有情况

回答by Thomas Modeneis

Maybe I'm a bit late here, but answers are a bit confusing. This one will always returns false when text is blank or with spaces but no chars.

也许我在这里有点晚了,但答案有点令人困惑。当文本为空白或有空格但没有字符时,此方法将始终返回 false。

boolean//Holding/Policy/PolNumber/child/text()[normalize-space()]

回答by David Dossot

What about using countto get the number of text nodes:

如何使用count获取文本节点的数量:

<expression-filter
    expression="#[xpath('count(//Holding/Policy/PolNumber/child::text())') != 0]"/>