1

I need do a check condition and if the condition not satisfies need to hard code the value. Below is my xml.

I need to check the condition like inside subroot- if ItemType=Table1 and ItemCondition=Chair1 then i have to give a hard coded value 'Proceed'( this hard coded value i will map to target side)

   <Root>
      <SubRoot>
          <ItemType>Table1</ItemType>
          <ItemCondition>Chair1</ItemCondition>
          <ItemValue>
              .......
           </ItemValue>
      </SubRoot>
      <SubRoot>
          <ItemType>Table2</ItemType>
           <ItemCondition>chair2</ItemCondition>
           <ItemValue>
               .......
           </ItemValue>
       </SubRoot>
   </Root>

I have tried like this but not seems to be working

      /Root/SubRoot[ItemType='Table1' and ItemCondition='Chair1']='Proceed'

Could you please anyone help on this. Thanks in advance.

Edited:

 if (SubRoot[ItemType ='Table1'  and ItemCondition ='Chair1']) then 'Proceed' else 'Decline'
star
  • 1,493
  • 1
  • 28
  • 61
  • @derp: Thank you. if the condition fails i have to send a hard coded value 'proceed'( i'm checking the 2 attribute value ItemType and Item Condition). I'm using a tool build on top of JAVA. Xpath version could be a latest one. i have proceeded with if else condition as well ( Edited in question) but no hope. – star Sep 16 '14 at 11:17

2 Answers2

3

Since you didn't specify whether you were transforming a document entirely or not, I'm going to answer with the assumption that you are working on XML elements individually in your code somewhere.

There is a trick that I use often for "if" conditions in XPath:

substring(<default_text>, 1 div <condition>)

What that does is basically give you the <default_text> if the right side ends up as 1 div <non-zero value>. So in your case, maybe you can do:

substring("Proceed", 1 div boolean(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))

That should give you "Proceed" for only elements that match those conditions you specified.

However, I noticed in your edit that you also want an "else" value. Once you understand the method I just showed you, then you can do some clever concat work to give you exactly what you want:

concat(
    substring("Proceed", 1 div boolean(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"])),
    substring("Decline", 1 div not(//Root/SubRoot[ItemType="Table1" and ItemCondition="Chair1"]))
)

So if the element meets those conditions, you should basically get concat("Proceed", "") which results in "Proceed", and concat("", "Decline") which results in "Decline" for an element that does not meet those conditions.

JoeLinux
  • 4,198
  • 1
  • 29
  • 31
  • Thank you for your support. I tried to implement first logic to test. But it is throwing error like `Arithmetic operator is not defined for arguments of types (xs:integer, xs:boolean)`. Could you please help on this. Thanks in advance – star Sep 16 '14 at 23:31
  • What language/library/etc are you using to implement XPath? – JoeLinux Sep 17 '14 at 14:40
  • i'm using Mule ESB , where i have datamapper to load source and target. source side we can also have a option of using xpath ( there where i'm trying ). version 3.5.1 – star Sep 17 '14 at 14:46
  • Sorry, I'm not familiar with Mule ESB. But I can assure you that it's valid XPath 1.0 syntax. I tested it with lxml. Ha, I also just found an SO answer where someone does the exact same thing in XPath: http://stackoverflow.com/a/7048664/639806. Maybe boolean() is conflicting with some internal Mule function? Do you pass the whole XPath as a string? – JoeLinux Sep 17 '14 at 18:46
  • 1
    Thanks in Tons and Tons. It helps me to resolve the solution. Yes, it a valid condition check. – star Sep 18 '14 at 11:00
0

XPath is used for selecting Nodes or NodeSets. I think you should check XQuery and generate new XML file.

It will be like

   for $x in /Root/SubRoot
   return   if ($x/ItemType='Table1' and $x/ItemCondition='Chair1')
   then <SubRoot>'Proceed'</SubRoot>
   else $x
dodo
  • 31
  • 6
  • Thank you for the quick reply. Then this will not be possible in xpath!. Because i have a limitation , can able to use only xpath – star Sep 16 '14 at 10:58