我在设计XQuery时遇到了麻烦。我的XML看起来像一个口袋妖怪数据库:
<species id="a0001">
<name></name>
<description></description>
<type></type>
<type></type> (can have different types)
<type></type>
<attack>
<has-attack id="p01"/>
<has-attack id="p02"/>
</attack>
<evolution>
<species id="a0002">
.
.
.
<evolution>
<species id="a0003">
.
.
.
</species>
</evolution>
</species>
<species id="a0004">
.
.
.
</species>
</evolution>
</species>
我要做的是让所有物种都具有“火”类型。
declare variable $tipo as xs:string := "Fire";
for $b in doc("doc.xml")/bd/species
let $nattacks:= count ($b/attacks)
where $b/type= $type
return <result>
{$b/@id}
{$b/name}
<na>{$nattacks}</na>
</result>
但我不知道如何进入“Evolutions”标签内的物种。 有什么帮助吗?
答案 0 :(得分:3)
@ har7是正确的,但可能更加整洁
declare variable $local:tipo as xs:string := "Fire";
for $b in doc("doc.xml")//species[type = $local:tipo]
return <result>
{$b/@id}
{$b/name}
<na>{count( $b/attacks )}</na>
</result>