XPATH选择所有非空子项加上未找到时的默认值

问题描述 投票:0回答:2

是否有XPATH返回所有非null someChild加上未找到值的默认值?

<someFather>
    <someChild/>
    <someChild/>
    <someChild>some value</someChild>
    <someChild/>
    <someChild>some other value</someChild>
    <someChild/>
</someFather>

我想得到:

""
""
some value
""
some other value
""

, 要么

"not-found"
"not-found"
some value
"not-found"
some other value
"not-found"
xpath xpath-2.0
2个回答
2
投票

请尝试以下表达式:

/someFather/(someChild/string(), '') 

3
投票
/someFather/someChild/(text()/string(), "not-found")[1]

这是经过仔细编写的,以避免违反“/”的RHS不能选择节点和原子值的混合的规则。在3.0中你可以使用“!”运营商:

/someFather/someChild ! (text(), "not-found")[1]
© www.soinside.com 2019 - 2024. All rights reserved.