2 Replies - 205 Views - Last Post: 18 May 2019 - 09:26 AM Rate Topic: -----

#1 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Can not retrieve correct output by using XQUERY

Posted 18 May 2019 - 09:16 AM

I have a table like this:


Posted Image

I am trying to retrieve these:
For each country (nationality), display the average goals of players. Sort the result by countries in a descending order

This is what I did:

xquery version "3.0";

declare namespace ext="http://www.altova.com/xslt-extensions";                                                                                          let $player := doc("player.xml")/soccerplayer/player
let $nation :=doc("player.xml")/soccerplayer/player/nationality
let $goal:=doc("player.xml")/soccerplayer/player/totalgoals
let $avg_goal:=avg($goal)
for $x in $player
                    return 
                   <result nationality="{$nation}" 
                          average="{$x/$avg_goal}"/>

the output is:
<result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/><result nationality="Brazil Brazil England England Italy Italy England Italy Spain Spain Spain" average="65.5454545454545"/>

my xml
<?xml version="1.0" encoding="UTF-8"?>

<soccerplayer>
	<player>
		<ID>1</ID>
		<name>Kaka</name>
		<position first="Forward" second="Midfielder"/>
		<team>AC Milan</team>
		<nationality>Brazil</nationality>
		<age>26</age>
		<totalgoals>62</totalgoals>
		<salary>143.000</salary>
	</player>
	<player>
		<ID>2</ID>
		<name>Ronaldinho</name>
		<position first="Forward" second="Midfielder"/>
		<team>Manchester United FC</team>
		<nationality>Brazil</nationality>
		<age>23</age>
		<totalgoals>84</totalgoals>
		<salary>90.000</salary>
	</player>
	<player>
		<ID>3</ID>
		<name>Wayne Rooney</name>
		<position first="Forward" second="Second Stiker"/>
		<team>Manchester United FC</team>
		<nationality>England</nationality>
		<age>23</age>
		<totalgoals>49</totalgoals>
		<salary>90.000</salary>
	</player>
		<player>
		<ID>4</ID>
		<name>Frank Lampard</name>
		<position first="Midfielder" second=""/>
		<team>Chelsea FC</team>
		<nationality>England</nationality>
		<age>30</age>
		<totalgoals>68</totalgoals>
		<salary>73.000</salary>
	</player>
	<player>
		<ID>5</ID>
		<name>Allessandro Del Piero</name>
		<position first="Second Striker" second=""/>
		<team>Juventus FC</team>
		<nationality>Italy</nationality>
		<age>34</age>
		<totalgoals>91</totalgoals>
		<salary>120.000</salary>
	</player>
	<player>
		<ID>6</ID>
		<name>Francesco Totti</name>
		<position first="Forward" second="Midfielder"/>
		<team>As Roma</team>
		<nationality>Italy</nationality>
		<age>32</age>
		<totalgoals>58</totalgoals>
		<salary>56.000</salary>
	</player>
	<player>
		<ID>7</ID>
		<name>Wayne Rooney</name>
		<position first="Forward" second="Second Striker"/>
		<team>Manchester United FC</team>
		<nationality>England</nationality>
		<age>23</age>
		<totalgoals>49</totalgoals>
		<salary>90.000</salary>
	</player>
	<player>
		<ID>8</ID>
		<name>Gianluigi Buffon</name>
		<position first="Goalkeeper" second=""/>
		<team>Juventus FC</team>
		<nationality>Italy</nationality>
		<age>31</age>
		<totalgoals>91</totalgoals>
		<salary>83.000</salary>
	</player>
	<player>
		<ID>9</ID>
		<name>Cesc Fabregas</name>
		<position first="Midfielder" second="Forward"/>
		<team>FC Barcelona</team>
		<nationality>Spain</nationality>
		<age>21</age>
		<totalgoals>37</totalgoals>
		<salary>33.000</salary>
	</player>
	<player>
		<ID>10</ID>
		<name>Fernando Torres</name>
		<position first="Forward" second=""/>
		<team>FC Barcelona</team>
		<nationality>Spain</nationality>
		<age>25</age>
		<totalgoals>60</totalgoals>
		<salary>75.000</salary>
	</player>
	<player>
		<ID>11</ID>
		<name>Carles Puyol</name>
		<position first="Centerback" second=""/>
		<team>FC Barcelona</team>
		<nationality>Spain</nationality>
		<age>30</age>
		<totalgoals>72</totalgoals>
		<salary>68.000</salary>
	</player>
</soccerplayer>



Bu it doesn't work correctly.

This post has been edited by shiftadelete: 18 May 2019 - 09:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Can not retrieve correct output by using XQUERY

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15226
  • View blog
  • Posts: 60,944
  • Joined: 12-June 08

Re: Can not retrieve correct output by using XQUERY

Posted 18 May 2019 - 09:20 AM

Please explain "doesn't work correctly".

I am not following what is supposed to be going on.
Was This Post Helpful? 0
  • +
  • -

#3 shiftadelete   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 04-November 16

Re: Can not retrieve correct output by using XQUERY

Posted 18 May 2019 - 09:26 AM

modi123_1

For each country (nationality), display the average goals of players. Sort the result by countries in a descending order
This what I want to retrieve. The output should be like;

<result nationality="Brazil" average="73"/>
<result nationality="Spain" average="65"/>
<result nationality="Italy" average="10"/>
.
.
.

As you can see in my output all countries are listing every in every row. and total average has taken. I want to get average of goals country by country

This post has been edited by shiftadelete: 18 May 2019 - 09:27 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1