在Xquery中按属性比较元素

时间:2014-12-01 20:09:37

标签: xml database xquery

到目前为止,我有一个名为resume的XML文档,除其他外,它包含以下方式的技能元素:

    <skills>
        <skill what = "C" level="0"></skill>
        <skill what = "Java" level="1"></skill>
        <skill what = "SQL" level="2"></skill>
        <skill what = "Lisp" level="3"></skill>
    </skills>

我想要运行的查询的目标如下:我想找到一对具有完全相同技能的简历,并且在“what”和“level”中匹配,并输出结果如这样:

resume1,resume2 resume1,resume3

这是我写的查询:

for $r1 in $resumes/resumes/resume, $r2 in $resumes/resumes/resume


where $r1/@rID != $r2/@rID and count($r1/skills/skill) = count($r2/skills/skill) 


return ($r1,$r2) 

我实际需要帮助实现$ r1和$ r2的@what和@level。我试过

data($r1/@what) = data($r1/skills/skill/@what) along with data($r1/skills/skill/@level) = data($r2/@level)

但我似乎无法获得所需的输出。有没有办法进行这种比较?非常感谢。

注意:我使用的是XML 1.0

1 个答案:

答案 0 :(得分:2)

通过将问题重新设计为查找技能,只有其中一个,问题变得相当容易解决。

let $skills1 :=    <skills>
        <skill what = "C" level="0"></skill>
        <skill what = "Java" level="1"></skill>
        <skill what = "SQL" level="2"></skill>
        <skill what = "Lisp" level="3"></skill>
        <skill what = "XQuery" level="4"></skill>
    </skills>
let $skills2 :=    <skills>
        <skill what = "C" level="0"></skill>
        <skill what = "Java" level="1"></skill>
        <skill what = "SQL" level="2"></skill>
        <skill what = "Lisp" level="3"></skill>
    </skills>

return
  for $skill in ($skills1, $skills2)/skill
  where not(
    $skills1/skill[deep-equal(., $skill)] and
    $skills2/skill[deep-equal(., $skill)]
  )
  return $skill

这将返回仅在技能组1(具有4级的XQuery)中的技能。如果在return调用中将整个flwor-expression包装在empty(...)子句中,则匹配技能集将获得true,如果有(任何),则为false不常见的成员。