Joel Mata
Joel Mata

Reputation: 524

XPATH to select all not null childs plus a default value when not found

Is there an XPATH to return all non-null someChild plus a default value for when the value on is not found?

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

I would like to get:

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

, or

"not-found"
"not-found"
some value
"not-found"
some other value
"not-found"

Upvotes: 1

Views: 98

Answers (2)

Michael Kay
Michael Kay

Reputation: 163418

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

This is carefully written to avoid breaking the rule that the RHS of "/" cannot select a mixture of nodes and atomic values. In 3.0 you could use the "!" operator:

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

Upvotes: 4

Try the following expression:

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

Upvotes: 3

Related Questions