vendredi 30 novembre 2018

xquery for loop and if condition and should only return the first value

I am trying to write an xquery which has if inside a for loop. Below is my request

<PersonIdentifiers> 
<PIR  PersonID="1834040001" IDType="N/A" IDNumber="NA" PrimaryID="true" Version="2" IdentiferFormat=""/> 
<PIR  PersonID="111111111" IDType="WEB" IDNumber="abc" PrimaryID="false" Version="3" IdentiferFormat=""/> 
<PIR  PersonID="222222222" IDType="N/A" IDNumber="xyz" PrimaryID="false" Version="4" IdentiferFormat=""/> 
<PIR  PersonID="333333333" IDType="WEB" IDNumber="aaaaaa" PrimaryID="false" Version="5" IdentiferFormat=""/> 
 <PIR  PersonID="444444444" IDType="WEB" IDNumber="aaaaaa" PrimaryID="false" Version="6" IdentiferFormat=""/> 
</PersonIdentifiers> 

What I want in the response is just 1 or the first "IDNumber" of the row where IDType=WEB I am currently using below xquery which has for loop to go through the array and return me value if IDType=WEB. But Ia m not bale to stop after the first successful value. How can i achieve that?

My current xquery:

        <ns2:account>

        <acc:persons>
            <acc:person>
                <acc:personID>{fn:data($PersonMaintenanceResponse/ns1:PersonIdentifiers/ns1:PIR/@IDType)}</acc:personID>
                {
                for $PIR at $position in $PersonMaintenanceResponse//ns1:PersonIdentifiers/ns1:PIR
                return
                if($PersonMaintenanceResponse//ns1:PersonIdentifiers/ns1:PIR[$position]/@IDType='WEB')
                then
                    if ($PersonMaintenanceResponse/ns1:PersonIdentifiers/ns1:PIR[$position]/@IDNumber)
                    then <acc:ID>{fn:data($PersonMaintenanceResponse/ns1:PersonIdentifiers/ns1:PIR[$position]/@IDNumber)}</acc:ID>
                    else ()
                else
                ()
                }

            </acc:person>
        </acc:persons>
    </ns2:account>

Response from my xquery

  <ns2:account>
<acc:persons xmlns:acc="***************">
  <acc:person>

    <acc:ID>abc</acc:ID>
    <acc:ID>aaaaaa</acc:ID>
    <acc:ID>aaaaaa</acc:ID>

  </acc:person>
</acc:persons>

Expected Response is, just to get the first ID , we do not need otehr ids, how can i stop after getting 1st id

  <ns2:account>
<acc:persons xmlns:acc="***************">
  <acc:person>
     <acc:ID>abc</acc:ID>
  </acc:person>
</acc:persons>

Aucun commentaire:

Enregistrer un commentaire