xQuery output by proceeding a heterogenous XML tree

Legendkilla

New Member
I try to proceed multiple XML files with xQuery (very limited experience so far). The important part of the XML can be presented like this:\[code\]<?xml version="1.0" encoding="UTF-8"?><ZOO xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><Mammals><marsupials> <zoo> <kangaroo> <number>25</number> <red_kangaroo> <number>1</number> </red_kangaroo> </kangaroo> </zoo></marsupials><ruminants> <giraffe> <number>10</number> <comments>Comment Text.</comments> </giraffe> <antelope> <number>20</number> <comments>Comment Text.</comments> </antelope> <elk> <number>2</number> <comments>Comment Text.</comments> </elk> <mouflon> <number>3</number> <comments>Comment Text.</comments> </mouflon> <ibex> <number>2</number> <comments>Comment Text.</comments> </ibex> <ox> <number>5</number> <comments>Comment Text.</comments> </ox> <other_ruminants> <other_ruminant> <name>bison</name> <number>1</number> <comments>Comment Text.</comments> </other_ruminant> <other_ruminant> <name>musk ox</name> <number>2</number> <comments>Comment Text.</comments> </other_ruminant> </other_ruminants></ruminants><rodents> <rodent> <name>hamster</name> <number>10</number> </rodent> <rodent> <name>squirrel</name> <number>15</number> <comments>Comment Text.</comments> </rodent> <rodent> <other_rodent> <other_rodent_name>porcupine</other_rodent_name> <comment>Comment Text.</comment> </other_rodent> <number>1</number> </rodent> <rodent> <other_rodent> <other_rodent_name>beaver</other_rodent_name> <comment>Comment Text.</comment> </other_rodent> <number>2</number> </rodent></rodents></Mammals></ZOO>\[/code\]As you see, the marsupials and the most of ruminants have their own tags. The rodents however have only the tag \[code\]<rodent>\[/code\] , they are named within with the tag \[code\]<name>\[/code\] and there are different rodents in each file.My attempt to make a audit of the files looks like this ( I work wirh BaseX 7.5):\[code\]for $z in /ZOO, $m in $z/Mammalsreturn<count><!-- file_id comes here later --><kangaroo>'{$z//kangaroo/number/text()}'</kangaroo><giraffe>'{$z//giraffe/number/text()}'</giraffe></count>\[/code\]... but I cannot do it with rodents part of course since the number of different rodents and their names are different in the different files.The desired output would be (for the shown file fragment):\[code\]<count> <!-- file_id comes here later --> <kangaroo>'25'</kangaroo> <giraffe>'10'</giraffe> <hamster>'10'</hamster> <squirrel>'15'</squirrel></count>\[/code\]Is it possible? Your help would be greatly appreciated!
 
Back
Top