Topic
5 replies Latest Post - ‏2012-10-04T20:58:57Z by SystemAdmin
JoeMarandola
JoeMarandola
4 Posts
ACCEPTED ANSWER

Pinned topic Need Help on XQuery Retrieval

‏2012-09-27T16:36:12Z |
I have a series of documents in one collection. Each document at the top level has the tag <flight-data> which has an aircraft ID as an attribute. Then within the document, there is lower level data under the tag <maintenance-raw-data> that contains fault IDs.

An example of two of the documents in the collection:
<flight-data aircraft-id = “1”>
<maintenance-raw-data fault-id=“0001”>
<maintenance-raw-data fault-id=“0002”>
<maintenance-raw-data fault-id=“0003”>
<maintenance-raw-data fault-id=“0001”>
</flight-data>

<flight-data aircraft-id = “2”>
<maintenance-raw-data fault-id=“0002”>
<maintenance-raw-data fault-id=“0003”>
<maintenance-raw-data fault-id=“0003”>
<maintenance-raw-data fault-id=“0004”>
<maintenance-raw-data fault-id=“0005”>
</flight-data>
So the intent of the xquery was to select all the data for one or more aircraft, then on that “total” selection, get a distinct list of all fault IDs. The xquery is as follows:

for $z in collection("brickdata2")/flight-data
where $z/@aircraft-id="1" or $z/@aircraft-id="2") return
for $x in distinct-values($z//maintenance-raw-data/@fault-id) return $x

Basically, I want the query to return the flight-data for both aircraft and then, on that total return, find the unique fault IDs. However, the second part operates on the unique fault IDs for each aircraft separately. It gives the unique fault IDs for the first aircraft and then the unique fault IDs for the second aircraft.

Query results with Aircraft 1 and Aircraft 2 in Where clause
Fault ID 0001
Fault ID 0002
Fault ID 0003
Fault ID 0002
Fault ID 0003
Fault ID 0004
Fault ID 0005
So we then changed the first line of the XQuery to “let $z := collection”. etc.” and then the query retrieving both aircraft works (you get 0001, 0002, 0003, 0004, 0005) “ but if you change the where clause to retrieve for only one aircraft (either one), you get the same answer (i.e., fault IDs for both aircraft).

Appreciate any help. Thanks in advance.
Updated on 2012-10-04T20:58:57Z at 2012-10-04T20:58:57Z by SystemAdmin
  • SystemAdmin
    SystemAdmin
    746 Posts
    ACCEPTED ANSWER

    Re: Need Help on XQuery Retrieval

    ‏2012-09-28T19:09:17Z  in response to JoeMarandola
    [0]
    for $z in collection("brickdata2")/flight-data
    where $z/@aircraft-id="1" or $z/@aircraft-id="2") return
    for $x in distinct-values($z//maintenance-raw-data/@fault-id) return $x
    Apart from a slight typo and that the format not being conducive to encourage of thinking straight, the output does show what is going wrong as it involves two distinct step due to the count of $z is actually two (or more if there are actually more files in the collection satisfying the where conditionals). So the simple/direct solution can look like this. (I add a couple of tags to make thing more readable in xml format.)
    
    <distinct-data> 
    { 
    
    for $y in distinct-values ( 
    
    for $z in collection(
    "brickdata2")/flight-data where ($z/@aircraft-id=
    "1" or $z/@aircraft-id=
    "2") 
    
    return ( 
    
    for $x in distinct-values($z
    //maintenance-raw-data/@fault-id) 
    
    return $x ) ) 
    
    return <fault-id>
    {$y
    }</fault-id> 
    } </distinct-data>
    

    [1]
    So we then changed the first line of the XQuery to “let $z := collection”. etc.” and then the query retrieving both aircraft works (you get 0001, 0002, 0003, 0004, 0005) “ but if you change the where clause to retrieve for only one aircraft (either one), you get the same answer (i.e., fault IDs for both aircraft).
    I am not sure I understand the last part... but using a let to begin with could well make the logic a bit clearer. But, it shouldn't have a draw back of giving a wrong result if the logic is correct, should it?
    
    <distinct-data> 
    { let $y:=( 
    
    for $z in collection(
    "brickdata2")/flight-data where ($z/@aircraft-id=
    "1" or $z/@aircraft-id=
    "2") (: where ($z/@aircraft-id=
    "1") :) 
    
    return $z )   
    
    for $x in distinct-values ($y/maintenance-raw-data/@fault-id) 
    
    return <fault-id>
    {$x
    }</fault-id> 
    } </distinct-data>
    


    Do they help to make thing clear up a bit?
    • JoeMarandola
      JoeMarandola
      4 Posts
      ACCEPTED ANSWER

      Re: Need Help on XQuery Retrieval

      ‏2012-10-04T19:32:05Z  in response to SystemAdmin
      Thanks, your 2nd query worked just fine.

      On the last part of my question, what was happening was when I retrieved for only aircraft 1 or I retrieved for aircraft 2, I got the answer for both aircraft.

      I also found out later that my query worked in all cases if I substituted the list of aircraft instead of using where clauses. I'm guessing that this puts together the set before executing.

      let $z := collection("brickdata2")/flight-data return
      for $x in distinct-values($z//maintenance-raw-data/@fault-id) return $x

      Obviously, I'm fairly new in this area - and I really appreciate your help!
      • JoeMarandola
        JoeMarandola
        4 Posts
        ACCEPTED ANSWER

        Re: Need Help on XQuery Retrieval

        ‏2012-10-04T19:34:05Z  in response to JoeMarandola
        Corrected query..

        let $z := collection("brickdata")/flight-data return
        for $x in distinct-values($z//maintenance-raw-data/@fault-id) return $x
        • JoeMarandola
          JoeMarandola
          4 Posts
          ACCEPTED ANSWER

          Re: Need Help on XQuery Retrieval

          ‏2012-10-04T19:41:04Z  in response to JoeMarandola
          Obviously, the system keeps removing a portion of the query I keep posing - probably because of the use of brackets

          Right after "flight-data" there is this term within brackets:

          @aircraft-id=("1","2")
          • SystemAdmin
            SystemAdmin
            746 Posts
            ACCEPTED ANSWER

            Re: Need Help on XQuery Retrieval

            ‏2012-10-04T20:58:57Z  in response to JoeMarandola
            I've given up using this forum because of (a) it's ideosyncratic editing conventions, and (b) the lack of a thriving community. I'd suggest using StackOverflow.