How to create a conditional Xpath statement?

I want to select an xml node with a conditional Xpath like -

xmlnode.SelectSingleNode("if (ns:substanceAdministration/ns:consumable/@typeCode == UNK) then evaluateThisXpath else evaluateOtherXpath") 

my concern is

 <drugID code="UNK"> <sub code="2232" /> </drugID> 

If the @code of the parent node is a UNK, then it must take the @code value of the child node; otherwise, it must take the value of the parent @code.

+8
xml xpath selectnodes
source share
2 answers

This should do the trick:

 (drugID[@code='UNK']/sub)|(drugID[@code<>'UNK') 

This is Xpath pseudocode, change it to your library language

+7
source share

Using

 drugId[@code = 'UNK']/sub/@code | drugId/@code[not(. = 'UNK')] 

which can be shortened:

 (drugId[@code = 'UNK']/sub | drugId[not(@code = 'UNK')])/@code 
+5
source share

All Articles