<html><head></head><body><div style="color:#000; background-color:#fff; font-family:HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px"><div id="yui_3_16_0_1_1448152061427_5857"><span id="yui_3_16_0_1_1448152061427_5860">Currently below works for me:</span></div><div id="yui_3_16_0_1_1448152061427_5857"><span><br></span></div><div id="yui_3_16_0_1_1448152061427_5857" class="">for $job in //job</div><div id="yui_3_16_0_1_1448152061427_5857" class="">order by xs:date(replace($job/StartDate, "(\d{2})-(\d{2})-(\d{4})", "$3-$1-$2")) descending</div><div id="yui_3_16_0_1_1448152061427_5857" class="">count $position</div><div id="yui_3_16_0_1_1448152061427_5857" class="">where $position eq 1</div><div id="yui_3_16_0_1_1448152061427_5857"></div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class="">return $job</div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class=""><br></div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class="">To this, I want to add:</div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class="">> Just need one more modification to this.  In the input if the StartDate is<br clear="none" id="yui_3_16_0_1_1448152061427_6667" class="">> blank, then I should consider that as the current job and return the job<br clear="none" id="yui_3_16_0_1_1448152061427_6669" class="">> that has blank as the output.<br clear="none" id="yui_3_16_0_1_1448152061427_6671" class=""><br></div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class=""><br></div><div id="yui_3_16_0_1_1448152061427_5857" dir="ltr" class="">Thank you for your response.</div><br>  <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_1_1448152061427_6266"> <div style="font-family: HelveticaNeue, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_1_1448152061427_6265"> <div dir="ltr" id="yui_3_16_0_1_1448152061427_6264"> <hr size="1"> <font size="2" face="Arial" id="yui_3_16_0_1_1448152061427_6263"> <b><span style="font-weight:bold;">From:</span></b> Christian Grün <christian.gruen@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> sudheshna iyer <sudheshnaiyer@yahoo.com> <br><b><span style="font-weight: bold;">Cc:</span></b> "talk@x-query.com" <talk@x-query.com><br> <b><span style="font-weight: bold;">Sent:</span></b> Sunday, November 15, 2015 4:35 AM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [xquery-talk] Xquery : Sort and get only the first record<br> </font> </div> <div class="y_msg_container" id="yui_3_16_0_1_1448152061427_6268"><br>How does your current XQuery expression look like?<br clear="none"><br clear="none"><br clear="none">On Sun, Nov 15, 2015 at 9:11 AM, sudheshna iyer <<a shape="rect" ymailto="mailto:sudheshnaiyer@yahoo.com" href="mailto:sudheshnaiyer@yahoo.com" id="yui_3_16_0_1_1448152061427_6270">sudheshnaiyer@yahoo.com</a>> wrote:<br clear="none">> Thank you all for the reply.<br clear="none">><br clear="none">> Just need one more modification to this.  In the input if the StartDate is<br clear="none">> blank, then I should consider that as the current job and return the job<br clear="none">> that has blank as the output.<br clear="none">><br clear="none">> If there are no jobs which have startDate as blank, then it should return<br clear="none">> the recent startdate job (for which we already have the response as shown in<br clear="none">> email chain,,)<br clear="none">><br clear="none">> For eg, in the below case, response should be the job element with id = 1:<br clear="none">> <?xml version="1.0" encoding="UTF-8"?><br clear="none">> <Request><br clear="none">>    <SessionInfo><br clear="none">>       <uid>qq</uid><br clear="none">>       <pwd>qq</pwd><br clear="none">>    </SessionInfo><br clear="none">>    <Param><br clear="none">>       <CustomerInfo><br clear="none">>          <Contact><br clear="none">>             <Number>123</Number><br clear="none">>             <Name>aaa bbb</Name><br clear="none">>             <Jobs><br clear="none">>                <job><br clear="none">>                   <Name>Analyst</Name><br clear="none">>                   <Id>1</Id><br clear="none">>                   <StartDate></StartDate><br clear="none">>                   <EndDate>1</EndDate><br clear="none">>                </job><br clear="none">>                <job><br clear="none">>                   <Name>Programmer</Name><br clear="none">>                   <Id>2</Id><br clear="none">>                   <StartDate>08-31-2015</StartDate><br clear="none">>                  <EndDate>2</EndDate><br clear="none">>                </job><br clear="none">>             </Jobs><br clear="none">>          </Contact><br clear="none">>       </CustomerInfo><br clear="none">>    </Param><br clear="none">> </Request><br clear="none">><br clear="none">><br clear="none">><br clear="none">><br clear="none">> ________________________________<br clear="none">> From: Ghislain Fourny <<a shape="rect" ymailto="mailto:g@28.io" href="mailto:g@28.io">g@28.io</a>><br clear="none">> To: Ronald Bourret <<a shape="rect" ymailto="mailto:rpbourret@rpbourret.com" href="mailto:rpbourret@rpbourret.com">rpbourret@rpbourret.com</a>><br clear="none">> Cc: "<a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a>" <<a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a>>; sudheshna iyer<br clear="none">> <<a shape="rect" ymailto="mailto:sudheshnaiyer@yahoo.com" href="mailto:sudheshnaiyer@yahoo.com">sudheshnaiyer@yahoo.com</a>><br clear="none">> Sent: Tuesday, November 3, 2015 4:51 AM<br clear="none">> Subject: Re: [xquery-talk] Xquery : Sort and get only the first record<br clear="none">><br clear="none">> Hi Ronald,<br clear="none">><br clear="none">> You can also use a count clause if you have XQuery 3.0! It makes it easier<br clear="none">> to read. Like so:<br clear="none">><br clear="none">> for $job in doc('x.xml')//job<br clear="none">> order by xs:date(replace($job/StartDate, "(\d{2})-(\d{2})-(\d{4})",<br clear="none">> "$3-$1-$2")) descending<br clear="none">> count $position<br clear="none">> where $position eq 1<br clear="none">> return $job<br clear="none">><br clear="none">> I hope it helps.<br clear="none">><br clear="none">> Kind regards,<br clear="none">> Ghislain<br clear="none">><br clear="none">><br clear="none">><br clear="none">><br clear="none">> On Mon, Nov 2, 2015 at 6:27 PM, Ronald Bourret <<a shape="rect" ymailto="mailto:rpbourret@rpbourret.com" href="mailto:rpbourret@rpbourret.com">rpbourret@rpbourret.com</a>><br clear="none">> wrote:<br clear="none">><br clear="none">> Can't use just use a predicate that returns the first element in the sorted<br clear="none">> sequence? Something like:<br clear="none">><br clear="none">>   let $seq = for $job in doc('x.xml')//job<br clear="none">>              order by<br clear="none">>                 xs:date(replace($job/StartDate,<br clear="none">>                         "(\d{2})-(\d{2})-(\d{4})", "$3-$1-$2")<br clear="none">>                        ) descending<br clear="none">>              return $job<br clear="none">>   return $seq[1]<br clear="none">><br clear="none">> (My apologies if the syntax isn't exact. I haven't written an XQuery for<br clear="none">> quite a while.)<br clear="none">><br clear="none">> -- Ron<br clear="none">><br clear="none">> On 11/2/2015 5:53 AM, sudheshna iyer wrote:<br clear="none">><br clear="none">> Thank you very much, Christian. Let me try this..<br clear="none">><br clear="none">><br clear="none">><br clear="none">> ------------------------------------------------------------------------<br clear="none">> *From:* Christian Grün <<a shape="rect" ymailto="mailto:christian.gruen@gmail.com" href="mailto:christian.gruen@gmail.com">christian.gruen@gmail.com</a>><br clear="none">> *To:* sudheshna iyer <<a shape="rect" ymailto="mailto:sudheshnaiyer@yahoo.com" href="mailto:sudheshnaiyer@yahoo.com">sudheshnaiyer@yahoo.com</a>><br clear="none">> *Cc:* "<a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a>" <<a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a>><br clear="none">> *Sent:* Sunday, November 1, 2015 4:26 PM<br clear="none">> *Subject:* Re: [xquery-talk] Xquery : Sort and get only the first record<br clear="none">><br clear="none">> If your XQuery processor supports XQuery Update, this would be one solution:<br clear="none">><br clear="none">>    copy $xml := doc('x.xml')<br clear="none">>    modify (<br clear="none">>      delete node subsequence(<br clear="none">>        for $job in $xml//job<br clear="none">>        order by xs:date(replace($job/StartDate,<br clear="none">>          "(\d{2})-(\d{2})-(\d{4})", "$3-$1-$2")<br clear="none">>        ) descending<br clear="none">>        return $job<br clear="none">>      , 2)<br clear="none">>    )<br clear="none">>    return $xml<br clear="none">><br clear="none">> Best,<br clear="none">> Christian<br clear="none">><br clear="none">><br clear="none">><br clear="none">><br clear="none">> On Sun, Nov 1, 2015 at 8:51 PM, sudheshna iyer <<a shape="rect" ymailto="mailto:sudheshnaiyer@yahoo.com" href="mailto:sudheshnaiyer@yahoo.com">sudheshnaiyer@yahoo.com</a><br clear="none">> <mailto:<a shape="rect" ymailto="mailto:sudheshnaiyer@yahoo.com" href="mailto:sudheshnaiyer@yahoo.com">sudheshnaiyer@yahoo.com</a>>> wrote:<br clear="none">>  > Team,<br clear="none">>  ><br clear="none">>  > I have an xml which has multiple jobs elements. I want to sort the<br clear="none">> jobs and<br clear="none">>  > want to output only the latest job. Note that <EndDate> can be empty<br clear="none">>  > indicating that it is the current job.<br clear="none">>  ><br clear="none">>  > Basically I want to order by jobs/job/EndDate in descending fashion and<br clear="none">>  > select only the first record..<br clear="none">>  ><br clear="none">>  > How do I do that using xquery?<br clear="none">>  ><br clear="none">>  > Input request:<br clear="none">>  > <?xml version="1.0" encoding="UTF-8"?><br clear="none">>  > <Request><br clear="none">>  >  <SessionInfo><br clear="none">>  >      <uid>qq</uid><br clear="none">>  >      <pwd>qq</pwd><br clear="none">>  >  </SessionInfo><br clear="none">>  >  <Param><br clear="none">>  >      <CustomerInfo><br clear="none">>  >        <Contact><br clear="none">>  >            <Number>123</Number><br clear="none">>  >            <Name>aaa bbb</Name><br clear="none">>  >            <Jobs><br clear="none">>  >              <job><br clear="none">>  >                  <Name>Analyst</Name><br clear="none">>  >                  <Id>1</Id><br clear="none">>  >                  <StartDate>01-01-2015</StartDate><br clear="none">>  >                  <EndDate>08-30-2015</EndDate><br clear="none">>  >              </job><br clear="none">>  >              <job><br clear="none">>  >                  <Name>Programmer</Name><br clear="none">>  >                  <Id>2</Id><br clear="none">>  >                  <StartDate>08-31-2015</StartDate><br clear="none">>  >                  <EndDate /><br clear="none">>  >              </job><br clear="none">>  >            </Jobs><br clear="none">>  >        </Contact><br clear="none">>  >      </CustomerInfo><br clear="none">>  >  </Param><br clear="none">>  > </Request><br clear="none">>  ><br clear="none">>  > Expected output:<br clear="none">>  ><br clear="none">>  > <?xml version="1.0" encoding="UTF-8"?><br clear="none">>  > <Request><br clear="none">>  >  <SessionInfo><br clear="none">>  >      <uid>qq</uid><br clear="none">>  >      <pwd>qq</pwd><br clear="none">>  >  </SessionInfo><br clear="none">>  >  <Param><br clear="none">>  >      <CustomerInfo><br clear="none">>  >        <Contact><br clear="none">>  >            <Number>123</Number><br clear="none">>  >            <Name>aaa bbb</Name><br clear="none">>  >            <Jobs><br clear="none">>  >              <job><br clear="none">>  >                  <Name>Programmer</Name><br clear="none">>  >                  <Id>2</Id><br clear="none">>  >                  <StartDate>08-31-2015</StartDate><br clear="none">>  >                  <EndDate/><br clear="none">>  >              </job><br clear="none">>  >            </Jobs><br clear="none">>  >        </Contact><br clear="none">>  >      </CustomerInfo><br clear="none">>  >  </Param><br clear="none">>  > </Request><br clear="none">>  ><br clear="none">>  > Your help is greatly appreciated.<br clear="none">><br clear="none">>  ><br clear="none">>  ><br clear="none">>  ><br clear="none">>  ><br clear="none">>  > _______________________________________________<br clear="none">>  > <a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a> <mailto:<a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a>><br clear="none">>  > <a shape="rect" href="http://x-query.com/mailman/listinfo/talk" target="_blank">http://x-query.com/mailman/listinfo/talk</a><br clear="none">><br clear="none">><br clear="none">><br clear="none">><br clear="none">><br clear="none">> _______________________________________________<br clear="none">> <a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a><br clear="none">> <a shape="rect" href="http://x-query.com/mailman/listinfo/talk" target="_blank">http://x-query.com/mailman/listinfo/talk</a><br clear="none">><br clear="none">><br clear="none">> ---<br clear="none">> This email has been checked for viruses by Avast antivirus software.<br clear="none">> <a shape="rect" href="https://www.avast.com/antivirus" target="_blank">https://www.avast.com/antivirus</a><div class="qtdSeparateBR"><br><br></div><div class="yqt5023547413" id="yqtfd98331"><br clear="none">><br clear="none">> _______________________________________________<br clear="none">> <a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a><br clear="none">> <a shape="rect" href="http://x-query.com/mailman/listinfo/talk" target="_blank">http://x-query.com/mailman/listinfo/talk</a><br clear="none">><br clear="none">><br clear="none">><br clear="none">> _______________________________________________<br clear="none">> <a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a><br clear="none">> <a shape="rect" href="http://x-query.com/mailman/listinfo/talk" target="_blank">http://x-query.com/mailman/listinfo/talk</a><br clear="none">><br clear="none">><br clear="none">><br clear="none">> _______________________________________________<br clear="none">> <a shape="rect" ymailto="mailto:talk@x-query.com" href="mailto:talk@x-query.com">talk@x-query.com</a><br clear="none">> <a shape="rect" href="http://x-query.com/mailman/listinfo/talk" target="_blank">http://x-query.com/mailman/listinfo/talk</a></div><br><br></div> </div> </div> </div></body></html>