2 Replies Latest reply: Jan 28, 2012 3:35 AM by odie_63 RSS

    How to return multiple tags (based on different checks) in a for loop?

    Fernando Nos
      Hi,

      I'm trying to return multiple elements, based on different checks that are done in the XQuery, within a for loop. But I'm getting syntax errors at any attempt to do this.

      This is the structure of what I'm trying to do:

                <ActionList>
           <ACtion>
      {
      let $total := count(...)
                     
      for $x in (1 to $total)
           let $lineItems := $someVariable/*:Items/*:Item[$x]
           return
                     
           {
                if(...)
                          <Qty1>{...}</Qty1>
                               else     ""
                     }
                     {
                          if(...)
                          <Qty2>{...}</Qty2>
                               else ""
                     }
                     {                                       if(...)
                <Qty3>{...}</Qty3>
                               else ""
                     }

                <LinesList>
                          {
                               let $totalcount(...)
                               for $y in (1 to $totalcount(...)
                                    let $DTL := $someVariable*:DTL[$y]
                               return
                               <Line>
                                    <Carrier>{...}</Carrier>
                                    <Path>{...}</Path>

                                    <CA>{
                                         if(...)
                                              data($$someVariable/*:CA)
                                         else
                                              ""
                                    }</CA>
                                    <RE>{
                                         if(...)
                                              data($someVariable*:CA[@XX="RG"])
                                         else
                                              ""
                                    }</RE>
                                    <Time>{
                                         if(...)
                                              data($someVariable*:CA[@XX="BN"])
                                         else
                                              ""}
                                                                       </Time>
                                    </Line>
                          }     
                          </LinesList>
                                                   </ACtion>
                                                   }
                                                   </ActionList>

      I'm not able to return elements without having their father being returned as well, I just want to iterate over those fields and, based on that verification, decide whether they should be returned or not.

      Can someone please advise?
        • 1. Re: How to return multiple tags (based on different checks) in a for loop?
          odie_63
          Hi Fernando,
          Can someone please advise?
          It would be easier for us to help if you could be more specific.

          Could you post the sample XML document you're working with?
          And the expected structure you require?
          • 2. Re: How to return multiple tags (based on different checks) in a for loop?
            odie_63
            An example anyway...

            Input document ($d) :
            <ns0:Items xmlns:ns0="http://my.company.org/my-namespace">
              <ns0:Item type="A">
                <ns0:Qty>2</ns0:Qty>
                <ns0:UnitPrice>10.00</ns0:UnitPrice>
                <ns0:DTL>
                  <ns0:Article>ART001</ns0:Article>
                  <ns0:DispatchDate>2012-01-20</ns0:DispatchDate>
                  <ns0:Destination direct="1">Location1</ns0:Destination>
                </ns0:DTL>
                <ns0:DTL>
                  <ns0:Article>ART002</ns0:Article>
                  <ns0:DispatchDate>2012-01-21</ns0:DispatchDate>
                  <ns0:Destination direct="1">Location2</ns0:Destination>
                </ns0:DTL>
              </ns0:Item>
              <ns0:Item type="B">
                <ns0:Mass>5</ns0:Mass>
                <ns0:Unit>kg</ns0:Unit>
                <ns0:DTL>
                  <ns0:Article>ART003</ns0:Article>
                  <ns0:DispatchDate>2012-01-20</ns0:DispatchDate>
                  <ns0:Destination direct="1">Location3</ns0:Destination>
                </ns0:DTL>
                <ns0:DTL>
                  <ns0:Article>ART004</ns0:Article>
                  <ns0:DispatchDate>2012-01-21</ns0:DispatchDate>
                  <ns0:Destination direct="1">Location4</ns0:Destination>
                </ns0:DTL>
                <ns0:DTL>
                  <ns0:Article>ART005</ns0:Article>
                  <ns0:DispatchDate>2012-01-22</ns0:DispatchDate>
                  <ns0:Destination direct="2">Location5</ns0:Destination>
                </ns0:DTL>
              </ns0:Item>
            </ns0:Items>
            XQuery :
            declare namespace ns0 = "http://my.company.org/my-namespace";
            <DispatchInfo>
            {
              for $i in $d/ns0:Items/ns0:Item
              return 
              <Parcel>
              {
                if ($i/@type = "A") 
                  then <Amount>{xs:decimal($i/ns0:Qty * $i/ns0:UnitPrice)}</Amount>
                  else <Weight>{concat($i/ns0:Mass, " ", $i/ns0:Unit)}</Weight>
              , for $j in $i/ns0:DTL
                return 
                <Article>
                {
                  <Num>{data($j/ns0:Article)}</Num>
                , <Dt>{data($j/ns0:DispatchDate)}</Dt>
                , if ($j/ns0:Destination/@direct = "1")
                    then <Dest>{data($j/ns0:Destination)}</Dest>
                    else ()
                }
                </Article>                  
              }
              </Parcel>
            }
            </DispatchInfo>
            Output :
            <DispatchInfo>
              <Parcel>
                <Amount>20</Amount>
                <Article>
                  <Num>ART001</Num>
                  <Dt>2012-01-20</Dt>
                  <Dest>Location1</Dest>
                </Article>
                <Article>
                  <Num>ART002</Num>
                  <Dt>2012-01-21</Dt>
                  <Dest>Location2</Dest>
                </Article>
              </Parcel>
              <Parcel>
                <Weight>5 kg</Weight>
                <Article>
                  <Num>ART003</Num>
                  <Dt>2012-01-20</Dt>
                  <Dest>Location3</Dest>
                </Article>
                <Article>
                  <Num>ART004</Num>
                  <Dt>2012-01-21</Dt>
                  <Dest>Location4</Dest>
                </Article>
                <Article>
                  <Num>ART005</Num>
                  <Dt>2012-01-22</Dt>
                </Article>
              </Parcel>
            </DispatchInfo>