This discussion is archived
3 Replies Latest reply: Oct 10, 2011 9:12 AM by tsuji RSS

XQuery For Loop Index variable not working

794954 Newbie
Currently Being Moderated
Hi,

I am using the below syntax for the for loop implementation in xquery


for $Reading at $Position in $MiddlewareMessage1/ns1:Data/ns1:Message/ns1:Reading
return
<ns0:Event latitude="{xs:decimal(data($MiddlewareInboundMessage1/ns1:Data/ns1:Message/ns1:Reading[$Position]/ns1:Lat))}"
                                   longitude="{xs:decimal(data($MiddlewareInboundMessage1/ns1:Data/ns1:Message/ns1:Reading[$Position]/ns1:Long))}">
                              
</ns0:Event>


}

Reading is a repeatable element under the source schema of Xquery at $MiddlewareMessage1/ns1:Data/ns1:Message and Event is a repeating element of the atrget schema of xquery.
When i test this using just one complex of Reading element it works fine. But if i try giving multiple records for Reading then i get an error - expected zero or one item, got two or more items
I am not sure what the problem is. Is there any error with the usage of for loop Position variable. Please suggest. Thanks in advance.
  • 1. Re: XQuery For Loop Index variable not working
    794954 Newbie
    Currently Being Moderated
    This got resolved. Thanks
  • 2. Re: XQuery For Loop Index variable not working
    pranavshah Newbie
    Currently Being Moderated
    Hi,

    How did it get resolved?
    I am trying to do something same but getting an error.
    Please let me know how did u iterate throught the array elemets.


    Regards
    Pranav
  • 3. Re: XQuery For Loop Index variable not working
    tsuji Journeyer
    Currently Being Moderated
    @pranav shah
    From what shown by the op (and that I suppose there may be a typo on the $Middle... variable), you should have include the proper parentheses to make the $Position mean what it meant. Like this.
    <ns0:Event 
      latitude="{xs:decimal(data( 
        ($MiddlewareInboundMessage1/ns1:Data/ns1:Message/ns1:Reading)[$Position]
        /ns1:Lat
        ))}" 
      longitude="{xs:decimal(data(
        ($MiddlewareInboundMessage1/ns1:Data/ns1:Message/ns1:Reading)[$Position]
        /ns1:Long
        ))}">
    </ns0:Event> 
    I make some line-breaks to make it seen clearer.

    Of course, the better way is to use the context itself and not to worry about the position.
    <ns0:Event2 
      latitude="{xs:decimal(data($Reading/ns1:Lat))}" 
      longitude="{xs:decimal(data($Reading/ns1:Long))}">
    </ns0:Event2>

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points