user9267428
user9267428

Reputation: 11

Xpath to use alternate element value if preferred value is nilled

I have a complication of the "usual" question of how to return a string value based on XPATH condition.

I've successfully used the following solutions before: Return a string value based on XPATH condition and Nested conditional if else statements in xpath

But now my XML is:

<Events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <EventDetails>
    <AccountingDate>2017-01-01</AccountingDate>
    <PaymentDate>2017-01-31</PaymentDate>
  </EventDetails>
  <EventDetails>
    <AccountingDate>2017-01-01</AccountingDate>
    <PaymentDate xsi:nil="true"/>
  </EventDetails>
</Events>

and I need to extract the PaymentDate if it has a value but otherwise substitute the AccountingDate i.e. 2017-01-31 for the first Event but 2017-01-01 for the second.

The problem is that the parser I'm forced to use is XPath 1.0 and does not recognize the nilled function and boolean(PaymentDate) is true if PaymentDate is nil.

I hope someone has a bright idea because I'm totally stumped.

Upvotes: 0

Views: 258

Answers (2)

VargaJoe
VargaJoe

Reputation: 175

You can also try this filtering

EventDetails[PaymentDate != '']/PaymentDate | EventDetails[PaymentDate = '']/AccountingDate

or on the actual level

PaymentDate[. != ''] | AccountingDate[../PaymentDate = '']

or if it is important to work when PaymentDate missing and xml data order is fixed

PaymentDate[. != ''] | AccountingDate[not(following-sibling::PaymentDate) or following-sibling::PaymentDate = '']

Upvotes: 0

user9267428
user9267428

Reputation: 11

Finally solved it after about 4 hours...

concat(substring(PaymentDate,   1 div    (boolean(PaymentDate) and PaymentDate!='')),
       substring(AccountingDate,1 div not(boolean(PaymentDate) and PaymentDate!='')))

(It also caters for the possibility that PaymentDate doesn't exist at all although in my case it always does.)

Now I just need to make it work with three possible dates!

Upvotes: 1

Related Questions