aldonair92
aldonair92

Reputation: 13

Accessing elements inside elements with XQuery

I'm having trouble designing an XQuery. My XML looks like a Pokemon database:

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

What I'm trying to do is get all species with the type "Fire".

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>

But I don't know how to access to the species inside the "Evolutions" label. Any help?

Upvotes: 1

Views: 144

Answers (1)

GSpringTech
GSpringTech

Reputation: 156

@har7 is correct but it could be much tidier

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>

Upvotes: 3

Related Questions