如何在xquery中分离返回?

时间:2015-05-21 02:41:42

标签: xml xml-parsing xquery return-value xquery-sql

从以下问题可以理解:

Retrieve a nonredundant list of dancer ids and names of those dancers who
took part in the Auckland 2008 competition. Sort by @dancer_id. The output
of your query should conform to the following DTD:
<!ELEMENT Auckland_2008_Dancers (she_dancers,
he_dancers)>
<!ELEMENT she_dancers (Dancer)+>
<!ELEMENT Dancer EMPTY>
<!ATTLIST Dancer id CDATA #REQUIRED name CDATA #REQUIRED>
<!ELEMENT he_dancers (Dancer)+>

我认为输出应该是这样的:

    <Auckland_2008_Dancers>
                <she_dancers>
                  <Dancer id="d6006" name="Sally Ann"/>
                </she_dancers>
                <she_dancers>
                  <Dancer id="d5005" name="Mimi"/>
                </she_dancers>
                <she_dancers>
                  <Dancer id="d1001" name="Anny"/>
                </she_dancers>
                <he_dancers>
                  <Dancer id="d9009" name="Gordon"/>
                </he_dancers>
                <he_dancers>
                  <Dancer id="d9008" name="Dimitrij"/>
                </he_dancers>         
                <he_dancers>
                  <Dancer id="d7007" name="James"/>
                </he_dancers>
                <he_dancers>
                  <Dancer id="d8007" name="Paul"/>
                </he_dancers>
        </Auckland_2008_Dancers>

但我的代码是:

let $c:=fn:doc("/db/Zahra/dancingCompetitions_XQuery_15.xml")/dancingCompetitions/Competition[contains(@date,"2008") and @place="Auckland"]/Dance/dancePair
let $c2:=fn:doc("/db/Zahra/dancingCompetitions_XQuery_15.xml")/dancingCompetitions/Pairs/Pair
let $h:=fn:doc("/db/Zahra/dancingCompetitions_XQuery_15.xml")/dancingCompetitions/Dancers/he_dancers/Dancer
let $s:=fn:doc("/db/Zahra/dancingCompetitions_XQuery_15.xml")/dancingCompetitions/Dancers/she_dancers/Dancer

for $c3 in distinct-values($c/@ref_pair)
let $m := $c2[@pair_id = $c3]

return
<Auckland_2008_Dancers>


{
for $c5 in $m/she/@ref_she_dancer
let $c6 := $s[@dancer_id=$c5]
order by $c6/@dancer_id

return

<she_dancers>
<Dancer id="{$c6/@dancer_id}"  name="{$c6/Name}"/>
</she_dancers>
}


{
for $c4 in $m/he/@ref_he_dancer
let $c7 := $h[@dancer_id=$c4]
order by $c7/@dancer_id

return
<he_dancers>
<Dancer id="{$c7/@dancer_id}"  name="{$c7/Name}"/>
</he_dancers>
}
</Auckland_2008_Dancers>

产生

<Auckland_2008_Dancers>
    <she_dancers>
        <Dancer id="d5005" name="Mimi"/>
    </she_dancers>
    <he_dancers>
        <Dancer id="d7007" name="James"/>
    </he_dancers>
</Auckland_2008_Dancers>
<Auckland_2008_Dancers>
    <she_dancers>
        <Dancer id="d6006" name="Sally Ann"/>
    </she_dancers>
    <he_dancers>
        <Dancer id="d9008" name="Dimitrij"/>
    </he_dancers>
</Auckland_2008_Dancers>
<Auckland_2008_Dancers>
    <she_dancers>
        <Dancer id="d1001" name="Anny"/>
    </she_dancers>
    <he_dancers>
        <Dancer id="d8007" name="Paul"/>
    </he_dancers>
</Auckland_2008_Dancers>
<Auckland_2008_Dancers>
    <she_dancers>
        <Dancer id="d6006" name="Sally Ann"/>
    </she_dancers>
    <he_dancers>
        <Dancer id="d9009" name="Gordon"/>
    </he_dancers>
</Auckland_2008_Dancers>

我做了什么? 我该如何纠正呢? 我已经尝试了很多东西来分离来自for的返回,有些产生了相同的结果,有些产生了错误。

1 个答案:

答案 0 :(得分:1)

<Auckland_2008_Dancers>元素移出循环。

let (: snip :)
return <Auckland_2008_Dancers>{
  for $c3 in distinct-values($c/@ref_pair)
  let $m := $c2[@pair_id = $c3]
  return
    (: snip :)
  }</Auckland_2008_Dancers>