XPATH选择所有不为空的子级,并在找不到时添加默认值

时间:2019-03-14 08:27:00

标签: xpath xpath-2.0

是否存在XPATH返回所有非空someChild以及在找不到on值时的默认值?

<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"

2 个答案:

答案 0 :(得分:3)

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

为避免违反“ /”的RHS无法选择节点和原子值的混合的规则,请仔细编写此代码。在3.0中,您可以使用“!”运算符:

/someFather/someChild ! (text(), "not-found")[1]

答案 1 :(得分:2)

尝试以下表达式:

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