用xquery在表中显示结果

时间:2013-11-27 17:38:27

标签: xquery

我有这个xml

<Edu>
 <Department>
  <Profesor>
  <first_name>Dr.aa</first_name>
  <last_name>bb</last_name>
</Profesor>

<Profesor>
  <first_name>Dr.cc</first_name>
  <last_name>dd</last_name>
</Profesor>

<Profesor>
  <first_name>Dr.ee</first_name>
  <last_name>ff</last_name>
</Profesor>
</Department>
<Course>
<name>programing</name>
<mosbat>4</mosbat>
<Class>1234</Class>
<Profesor>
  <first_name>Dr.aa</first_name>
  <last_name>bb</last_name>
</Profesor>
<time>12:30:00</time>
<Student>
    <name>jack</name>
    <Std_ID>12345678</Std_ID>
    <Degree>MS</Degree>
</Student>

<Student>
  <name>parisa</name>
  <Std_id>54321876</Std_id>
  <Degree>BS</Degree>
</Student> 
</Course>
<Course>
<name>network</name>
<mosbat>9</mosbat>
<Class>6789</Class>
<Profesor>
  <first_name>Dr.ee</first_name>
  <last_name>ff</last_name>
</Profesor>
<time>09:30:00</time>
<Student>
  <name>jack</name>
  <Std_ID>12345678</Std_ID>
  <Degree>MS</Degree>
</Student>
</Course>
<Course>
<name>Database</name>
<mosbat>3</mosbat>
<Class>4321</Class>
<Profesor>
  <first_name>Dr.cc</first_name>
  <last_name>dd</last_name>
</Profesor>
<time>10:30:00</time>

<Student>
    <name>jack</name>
    <Std_ID>12345678</Std_ID>
    <Degree>MS</Degree>
</Student>

<Student>
    <name>caty</name>
    <Std_ID>87654321</Std_ID>
    <Degree>MS</Degree>
</Student> 
</Course>
<Course>
<name>data</name>
<mosbat>8</mosbat>
<Class>2341</Class>
<Profesor>
  <first_name>Dr.aa</first_name>
  <last_name>bb</last_name>
</Profesor>
<time>11:30:00</time>

  <Student>
    <name>jack</name>
    <Std_ID>12345678</Std_ID>
    <Degree>MS</Degree>
  </Student>

  <Student>
    <name>caty</name>
    <Std_ID>87654321</Std_ID>
    <Degree>MS</Degree>
  </Student>   
</Course>
</Edu> 

我希望每个profesor有一个表,表的第一行是profesor name,第二行是profesor所拥有的课程的信息。并且保留表的行是每个字段的值。我有这个xquery,但它只绘制了2行的表,每个字段的值都是空的 我的xquery是:

for $r in doc("XMLFile_Q2.xml")/Edu/Department/Profesor
 return
  <table  border="2" width="100%">
  <tr>
   <th>{$r[first_name and last_name]}</th>
  </tr>

  <tr>
<th>course name</th>
<th>class</th>
<th>capacity</th>
<th>time</th>
<th>student</th>
  </tr>
  {for $a in doc("XMLFile_Q2.xml")/Edu/Department/Course
where $a/Profesor[first_name and last_name]=$r[first_name and last_name]
return
 <tr>
    <td>{$a/name/text()}</td>
    <td>{$a/Class/text()}</td>
    <td>{$a/mosbat/text()}</td>
    <td>{$a/time/text()}</td>
    <td>{$a/Student/name/text()}</td>

  </tr>}
 </table>

1 个答案:

答案 0 :(得分:1)

您的XQuery中存在错误:

for $a in doc("XMLFile_Q2.xml")/Edu/Department/Course

应该是

for $a in doc("XMLFile_Q2.xml")/Edu/Course

(或者,或者你的文件格式错误)