Sunday, August 4, 2013

XPath query string returns zero node. The assign activity of the to node query is returning zero node. Either the to node data or the xpath query in the to node was invalid

XPath query string returns zero node. The assign activity of the to node query is returning zero node. Either the to node data or the xpath query in the to node was invalid

Very common error and we are sure, every developer must have seen this.
Normally we get this error when we are looping over a variable and mapping to a target variable. In first loop, assign operation is successful and then we get the above error when loop runs for the second time.

To resolve this, we use append option while assigning the variable
But if there are more than one variables to be assigned, then there are 2 ways:

One option is to create a temp variable of output type and complete the assignment and then in the end, map it to the output using assign and append option. But this method has an overhead of temp variable.

Second option which we are going to explain is what we recommend. It doesn’t require any temp variable to be managed and is lot easier!!!

We will begin with the below example wherein we are building a basic calculator.

The sample xsd which we have used is below:


Input:

  <ns1:calcInput>
    <ns1:input>
     <ns1:number1>1</ns1:number1>
     <ns1:number2>1</ns1:number2>
     <ns1:operation>add</ns1:operation>
    </ns1:input>
    <ns1:input>
     <ns1:number1>1</ns1:number1>
     <ns1:number2>3</ns1:number2>
     <ns1:operation>add</ns1:operation>
    </ns1:input>
    <ns1:input>
     <ns1:number1>1</ns1:number1>
     <ns1:number2>4</ns1:number2>
     <ns1:operation>add</ns1:operation>
    </ns1:input>
   </ns1:calcInput>

Output:

<result>
  <output>
   <number1>1</number1>
   <number2>4</number2>
   <operation>add</operation>
   <result>5</result>
  </output>
  <output>
   <number1>1</number1>
   <number2>1</number2>
   <operation>add</operation>
   <result>2</result>
  </output>
  <output>
   <number1>1</number1>
   <number2>3</number2>
   <operation>add</operation>
   <result>4</result>
  </output>
  <output>
   <number1>1</number1>
   <number2>1</number2>
   <operation>add</operation>
   <result>2</result>
  </output>
 </result>           

The flow of the code looks something as shown below:


First loop in the code will do a normal mapping and the operation using the loopCounter variable






Second time, the flow goes in otherwise wherein we append the output variable to the output variable itself as shown below:



After that, we assign the input variable to the output but using loopCounter variable at the input and index 1 on the output side as shown below:


That’s it and you should get the output as expected.

I have the sample code with me. Email us if you need the sample. 

Thanks and Happy Coding J J J

Cheers,
LetsLearnOracleSOA Team

No comments:

Post a Comment