1 Reply Latest reply: May 18, 2013 9:22 PM by 991448 RSS

    XQuery Question: FLWOR expressions?

    1009726
      Help. I am a bit confused and stuck on this question regarding XQuery.

      Here's the XML for it.
      URL: http://s3.amazonaws.com/spark-public/db/docs/courses-noID.xml

      Return course numbers of courses that have the same title as some other course.

      Please explain to me how to obtain the answer STEP-BY-STEP and the ANSWER. :) Good luck.

      - Nathan
        • 1. Re: XQuery Question: FLWOR expressions?
          991448
          Here is what you can try:-
          Lets say the input is assigned to $out variable

          let $output := $out//*:Title (:in this step get the list of all titles:)

          let $outputNew:= <t>{for $x in 1 to count($out//*:Title)
                                          for $y in  $x+1 to count($out//*:Title)
                                               where $output[$x]=$output[$y]
                                             return
                                             <countgreaterthanone>{$output[$x]}</countgreaterthanone>
                                             
                                             }</t> (:through this loop get the list of all titles which count of more than one, loop through the sequence and then find if two elements match means that their occurenceis more than once:)
                                             
               let $moreThanOnetitles:= fn:data($outputNew//*:Title) (:get the sequence of all titles with count more than one)
               
               let $finalOut:=      <temp>{
               for $x in $out//*:Course
               let $title:= data($x/*:Title)
                         where some $titleinseq in $moreThanOnetitles satisfies deep-equal($titleinseq,$title)
                         
               return
               <out>
               <ttt>{$title}</ttt>
               <coursenumber>{$x/@Number}</coursenumber>
               </out>
               
                         
               }</temp>                         
          return $finalOut

          (:through this FLOWR loop through all courses and find if their titles is there in the sequence of $morethantitles then return the title and coursenumber:)