1

I have an xpath which contains an if statement. I want to evaluate it with c#. If i dont have any functions in xpath I use

node.SelectSingleNode(xpath);

When I try to do this with if statement I get an invalid token error.

This is my xpath:

if (substring-before(substring-after(//IzdaniRacunEnostavni/@xsi:noNamespaceSchemaLocation,'http://www.gzs.si/e-poslovanje/sheme/'),'_EnostavniRacun.xsd')='eSLOG_1-6') then //Postavka/StevilkaVrstice[text()!='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='203']/../ZnesekPostavke else     //Postavka/StevilkaVrstice[text() !='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='66']/../ZnesekPostavke

If I test this xpath with http://videlibri.sourceforge.net/cgi-bin/xidelcgi it shows as valid.

n32303
  • 831
  • 1
  • 10
  • 25
  • Is http://stackoverflow.com/questions/971067/is-there-an-if-then-else-statement-in-xpath what you are looking for? Note that validator you've added is for XPath 2.0 which is not part of .Net Framework's Xml classes. – Alexei Levenkov Sep 03 '14 at 06:19
  • Hmm, I believe .NET 4.5 supports xpath 2.0... – n32303 Sep 03 '14 at 06:22
  • Shouldn't it be "eq" instead of equals to sign(=) in the IF expression – Vizard Sep 03 '14 at 06:24
  • @Vizard no, it doesnt work either.. – n32303 Sep 03 '14 at 06:26
  • What is you link for "XPath 2.0 supported by 4.5"? Mine is [XML Standards Reference](http://msdn.microsoft.com/en-us/library/ms256177%28v=vs.110%29.aspx): "Microsoft...provides developer support for ... The XML Path Language *(XPath) 1.0*," – Alexei Levenkov Sep 03 '14 at 06:33
  • hm.. well, that sucks.. is there any other way to achieve same effect with xpath 1.0? – n32303 Sep 03 '14 at 06:35
  • I don't think there is anything better than `concat` I've linked in first comment. There are non-Microsoft parsers for .Net with support of XPath 2.0/XSLT 2.0 if you really need it... (I personally would use LINQ-to-XML or plain C# code instead of complicated XPath). – Alexei Levenkov Sep 03 '14 at 06:51

1 Answers1

0

Translate the if-else part of your Xpath into C# statement maybe your best bet when using C# and XPath 1.0. Formatted version of your xpath if-else block looks like this :

if (substring-before(substring-after(//IzdaniRacunEnostavni/@xsi:noNamespaceSchemaLocation,'http://www.gzs.si/e-poslovanje/sheme/'),'_EnostavniRacun.xsd')='eSLOG_1-6') 

then 

//Postavka/StevilkaVrstice[text()!='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='203']/../ZnesekPostavke 

else    

//Postavka/StevilkaVrstice[text() !='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='66']/../ZnesekPostavke

Try to translate them into this C# if-else block :

var xpathIfResult = node.SelectSingleNode("//IzdaniRacunEnostavni[substring-before(substring-after(@xsi:noNamespaceSchemaLocation,'http://www.gzs.si/e-poslovanje/sheme/'),'_EnostavniRacun.xsd')='eSLOG_1-6']");
if(xpathIfResult != null)
{
    result = node.SelectSingleNode("//Postavka/StevilkaVrstice[text() !='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='66']/../ZnesekPostavke");
}
else
{
    result = node.SelectSingleNode("//Postavka/StevilkaVrstice[text() !='']/../../ZneskiPostavke/VrstaZneskaPostavke[text() ='66']/../ZnesekPostavke");
}
har07
  • 88,338
  • 12
  • 84
  • 137