简体   繁体   中英

Comparing elements by attribute in Xquery

so far I have an XML document called resume that, among other things, contains skill elements in the following manner:

    <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>

The objective of my query I want to run is the following: I want to find pairs of resumes that possess exactly the same number of skills, and that match in both "what" and "level", and output results such as this:

resume1, resume2 resume1, resume3

This is the query I have written:

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) 

I need help in actually comapring both $r1 and $r2's @what and @level. I have tried with

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

but i can't seem to get the desired output. Is there a way to make this comparison? Thanks very much.

Note: I am using XML 1.0

By reformulating your problem to find skills, that only one of them has , the problem gets rather easy to solve.

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

This will return the skill that is only in skill set 1 (XQuery with level 4). If you wrap the whole flwor-expression in the return clause in an empty(...) call, you will get true for matching skill sets, or false if they have (any) uncommon members.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM