-2

Need help with a homework assignment. I have two XML files: Person.xml and passedExams.xml

Person.xml form ->

<listWrapper>
   <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="person">
      <address>Random street 2, Neverland</address>
      <telephoneNumber>555-1-612-9999</telephoneNumber>
      <name>Captain</name>
      <sid>35168589</sid>
      <surname>Obvious</surname>
    </items>
    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="person">
      <address>Random avenue, Neverland</address>
      <telephoneNumber>555-1-123-9999</telephoneNumber>
      <name>Gabe</name>
      <sid>36431731</sid>
      <surname>Newell</surname>
   </items>
 </listWrapper>

passedExams.xml form ->

<listWrapper>
   <items xsi:type="passedExams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <id>
          <ExamID>BM3409</ExamID>
          <sid>36431731</sid>
      </id>
      <month>7</month>
      <grade>4</grade>
   </items> 
   <items xsi:type="passedExams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <id>
           <ExamID>MV7402</ExamID>
           <sid>36441189</sid>
       </id>
       <month>7</month>
       <grade>4</grade>
   </items>
 </listWrapper>

Using those two files I'm trying to list all people who have passed x exams in y month of the year. For example, fetch all people who have passed 2 exams in 10th month of the year. I'm having trouble in writing a good query cause I'm not good with it. This is what I got so far:

for $exam in doc("pasedExams.xml")/listWrapper//items[month = 1]
where $person in doc("Person.xml")/listWrapper/items[$exam/sid = $person/sid] satisfies  (count($exam/sid) = 2)
return 
     <info>
        $person
     </info>

Here x = 1 and y = 2 just for the demo. The user can choose the parameters when the program starts. To clarify logic behind this, I'm trying to iterate through exams which have happened in January and then I'm trying to match SID (student ID) from the exam with one from Person.xml and then I'm counting the number of SID's occurrences in passedExams because the number of SID's occurrences is the number of passedExams by that student. When I run this in Stylus Studio is shows this error: [DataDirect][XQuery][err:XPST0003]Error at line 2, column 14. Expected "return", but encountered "in".

How can I make this work?

4

2 回答 2

1

Your error statement clearly states a syntax error in your code. You can not use that in construct in the where clause. Also, you FWLOR expression does not represent what you want to achieve: You want to output all users, but you iterate over each exam although this is simply a required condition. So it should be more like that:

for $user in doc("Person.xml")/listWrapper/items
where count(doc("passedExams.xml")/listWrapper/items[id/sid = $user/sid and month = 1]) >= 2
return $user

I don't now, but I would guess that grade also determines if the person actually passed or failed the axam, so you might have to have another condition.

于 2013-05-18T16:04:51.390 回答
1

你想要这样的东西

let $numExams    := 0,
    $month       := 1,
    $Persons     := doc("file:///c:/temp/delete/Person.xml"),
    $PassedExams := doc("file:///c:/temp/delete/passedExams.xml")
 return
    for $p in $Persons/listWrapper/items
     return
        if($PassedExams/listWrapper/items
                     [xs:integer(month) eq $month and id/sid eq $p/sid]
                        [if($numExams gt 0)
                           then position() ge $numExams
                           else false()
                     ]
          or
           $numExams le 0
          and not($PassedExams/listWrapper/items
                          [xs:integer(month) eq $month and id/sid eq $p/sid])          )
           then
             (
              <info>
                {$p}
              </info>
              )
           else ()

Person.xml当使用提供的(纠正了几个错误)应用此 XQuery(以及纯 XPath 3.0 表达式)时:

<listWrapper>
   <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="person">
      <address>Random street 2, Neverland</address>
      <telephoneNumber>555-1-612-9999</telephoneNumber>
      <name>Captain</name>
      <sid>35168589</sid>
      <surname>Obvious</surname>
    </items>
    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="person">
      <address>Random avenue, Neverland</address>
      <telephoneNumber>555-1-123-9999</telephoneNumber>
      <name>Gabe</name>
      <sid>36431731</sid>
      <surname>Newell</surname>
   </items>
</listWrapper>

并在更正和稍微修改后PassedExams.xml

<listWrapper>
   <items xsi:type="passedExams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <id>
          <ExamID>BM3409</ExamID>
          <sid>36431731</sid>
      </id>
      <month>1</month>
      <grade>4</grade>
   </items>
   <items xsi:type="passedExams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
       <id>
           <ExamID>MV7402</ExamID>
           <sid>36441189</sid>
       </id>
       <month>7</month>
       <grade>4</grade>
   </items>
   <items xsi:type="passedExams" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <id>
          <ExamID>CL3102</ExamID>
          <sid>36431731</sid>
      </id>
      <month>1</month>
      <grade>4</grade>
   </items>
</listWrapper>

产生了想要的正确结果

<info>
 <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="person">
      <address>Random avenue, Neverland</address>
      <telephoneNumber>555-1-123-9999</telephoneNumber>
      <name>Gabe</name>
      <sid>36431731</sid>
      <surname>Newell</surname>
 </items>
</info>
于 2013-05-18T16:39:09.147 回答