在两个 XML 文档上使用 if 语句

Using if statements on two XML documents

我有两个 XML 文档 Students.xml 和 Admission.xml,我需要一个 xquery 来 return 已经完成 30 天的学生的学生 ID 和学生姓名入学日期。我很困惑如何加入他们以及使用条件来提出,请帮助。

Student.xml

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<StudentDetails>
    <Student_ID>1</Student_ID>
    <Student_Name>Mark</Student_Name>
</StudentDetails>
<StudentDetails>
    <Student_ID>2</Student_ID>
    <Student_Name>Robin</Student_Name>
</StudentDetails>
<StudentDetails>
    <Student_ID>3</Student_ID>
    <Student_Name>Charlie</Student_Name>
</StudentDetails>
</root>

Admission.xml

<?xml version="1.0" encoding="UTF-8" ?>
<root>
<AdmissionDetails>
    <Admission_ID>1</Admission_ID>
    <Admission_Date>2017-10-06</Admission_Date>
    <Admission_Status>Admitted</Admission_Status>
    <Student_ID>3</Student_ID>
</AdmissionDetails>
<AdmissionDetails>
    <Admission_ID>2</Admission_ID>
    <Admission_Date>2017-04-14</Admission_Date>
    <Admission_Status>Admitted</Admission_Status>
    <Student_ID>2</Student_ID>
</AdmissionDetails>
<AdmissionDetails>
    <Admission_ID>3</Admission_ID>
    <Admission_Date>2017-08-16</Admission_Date>
    <Admission_Status>Admitted</Admission_Status>
    <Student_ID>1</Student_ID>
</AdmissionDetails>

到目前为止我的代码是这样的,但它给了我一个重复值的结果,迫切需要帮助。

for $sid in doc("Student.xml")/root/StudentDetails/Student_ID
for $aid  in doc("Admission.xml")/root/AdmissionDetails/Student_ID
for $x in doc("Admission,xml")/root/AdmissionDetails/Admission_Date
where $sid = $aid and $x >current-date() + xs:dayTimeDuration("P30D")
return  $sid

这是结果

<?xml version="1.0" encoding="UTF-8"?>
<Student_ID>1</Student_ID>
<Student_ID>1</Student_ID>
<Student_ID>10</Student_ID>
<Student_ID>10</Student_ID>

您可以遍历 Students.xml 文档中的每个 StudentDetails,然后过滤 StudentDetails/Student_ID 值等于 Admission.xml AdmissionDetails 元素的 Student_ID 并且有一个 Admission_Date 符合您的标准(您确定这是正确的日期过滤器吗?您的描述说完成日期在入学后 30 天内,但示例数据没有完成日期,只有入学日期),然后 return 来自满足 WHERE 子句条件的学生的 Student_IDStudent_Name

for $student in doc("Student.xml")/root/StudentDetails
where doc("Admission.xml")/root/AdmissionDetails[
        Student_ID = $student/Student_ID and 
        xs:date(Admission_Date) < current-date() + xs:dayTimeDuration("P30D") 
      ]
return 
  ($student/Student_ID, $student/Student_Name)

XQuery 规范包含 "inner" 和 "outer" joins 的示例,这些示例可能具有参考意义。

我认为您陷入了这样的陷阱,即认为每个查询都必须是 FLWOR 表达式(这通常表明您写 SQL 的时间太长了)。

我会这样写:

    let $S30 := doc("Admission.xml")/*/Admission_Details
        [xs:date(Admission_Date) < current-date() + xs:dayTimeDuration("P30D")]
        /Admission_ID
    return doc("Student.xml")/*/StudentDetails[Student_ID = $S30]