Consider the following XML:
<Ships>
<Class name="Kongo">
<Ship name="h" launched="1915"/>
<Ship name="h1" launched="1920">
<Battle outcome="sunk">sunk1</Battle>
</Ship>
</Class>
<Class name="Kongo2">
<Ship name="h2234234" launched="1941">
<Battle outcome="sunk">ok</Battle>
</Ship>
<Ship name="h123123" launched="1941">
<Battle outcome="ok">ok</Battle>
</Ship>
<Ship name="h3" launched="1941"/>
</Class>
<Class name="Kongo2">
<Ship name="h4" launched="1917"/>
<Ship name="h5" launched="1917"/>
<Ship name="h5" launched="1910"/>
</Class>
</Ships>
I need to find the names of the classes with at least 3 ships using XQuery. How can I go about doing it?
CodePudding user response:
You can use the XPath
/Ships/Class[count(Ship) > 2]/@name
Or a FLWOR approach:
let $file := doc("input.xml") return
for $r in $file/Ships/Class
where count($r/Ship) > 2
return <result>{$r/@name}</result>