Count number of words in each attribute
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
How would I go about counting the number of words in a specific attribute for each element?
Example: I want to count how many cities each country has, and cities are attributes like this: world/country/[@cities]. I then want to display this as a new list which displays each country and the number of cities that each country has.
I have tried this:
for $country in doc("world.xml")/world/country
let $neighbors := tokenize($country/@cities, 's+')
let $count := count($neighbors)
order by $count
return ($country/name, $count)
But how do I get max of the count?
xml xpath xquery
add a comment |
How would I go about counting the number of words in a specific attribute for each element?
Example: I want to count how many cities each country has, and cities are attributes like this: world/country/[@cities]. I then want to display this as a new list which displays each country and the number of cities that each country has.
I have tried this:
for $country in doc("world.xml")/world/country
let $neighbors := tokenize($country/@cities, 's+')
let $count := count($neighbors)
order by $count
return ($country/name, $count)
But how do I get max of the count?
xml xpath xquery
add a comment |
How would I go about counting the number of words in a specific attribute for each element?
Example: I want to count how many cities each country has, and cities are attributes like this: world/country/[@cities]. I then want to display this as a new list which displays each country and the number of cities that each country has.
I have tried this:
for $country in doc("world.xml")/world/country
let $neighbors := tokenize($country/@cities, 's+')
let $count := count($neighbors)
order by $count
return ($country/name, $count)
But how do I get max of the count?
xml xpath xquery
How would I go about counting the number of words in a specific attribute for each element?
Example: I want to count how many cities each country has, and cities are attributes like this: world/country/[@cities]. I then want to display this as a new list which displays each country and the number of cities that each country has.
I have tried this:
for $country in doc("world.xml")/world/country
let $neighbors := tokenize($country/@cities, 's+')
let $count := count($neighbors)
order by $count
return ($country/name, $count)
But how do I get max of the count?
xml xpath xquery
xml xpath xquery
edited Nov 16 '18 at 14:29
John Weeeek
asked Nov 16 '18 at 14:00
John WeeeekJohn Weeeek
255
255
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
There are a few approaches one might take. Following your start to its logical conclusion, you've already ordered your items by $count
, so you can bind these ordered items to a variable and retrieve the greatest one using the fn:last()
function. You didn't provide source data, so here's a complete example demonstrating the approach:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
</people>
let $ordered :=
(
for $person in $people/person
order by $person/@age cast as xs:integer
return
$person
)
let $oldest := $ordered[last()]
return
$oldest
This will return the Lucy entry. (If we hadn't cast the values as xs:integer
we would've gotten Billy.)
However, it's possible there will be two or more people who have the same greatest age. To return all entries with the maximum age, you could first find the maximum value, then select the items with that value:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
let $ages := $people/person/@age
let $max := max($ages)
let $oldest := $people/person[@age = $max]
return
$oldest
This will return the Lucy and Holly entries. (We didn't have to cast the values as integer in this case because the fn:max()
function casts all values of type xs:anyAtomicType
as xs:double
; see https://www.w3.org/TR/xpath-functions-31/#func-max.)
Or, in a more compact form:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
return
$people/person[@age = max($people/person/@age)]
add a comment |
Guessing from your code, I think max
with for
should work using XPath 2.0+
max(for $country in doc("world.xml")/world/country
return count(tokenize($country/@cities, 's+')))
and then select the corresponding nodes
/world/country[count(tokenize(@cities, 's+')) = max(for $country in /world/country
return count(tokenize($country/@cities, 's+')))]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339333%2fcount-number-of-words-in-each-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are a few approaches one might take. Following your start to its logical conclusion, you've already ordered your items by $count
, so you can bind these ordered items to a variable and retrieve the greatest one using the fn:last()
function. You didn't provide source data, so here's a complete example demonstrating the approach:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
</people>
let $ordered :=
(
for $person in $people/person
order by $person/@age cast as xs:integer
return
$person
)
let $oldest := $ordered[last()]
return
$oldest
This will return the Lucy entry. (If we hadn't cast the values as xs:integer
we would've gotten Billy.)
However, it's possible there will be two or more people who have the same greatest age. To return all entries with the maximum age, you could first find the maximum value, then select the items with that value:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
let $ages := $people/person/@age
let $max := max($ages)
let $oldest := $people/person[@age = $max]
return
$oldest
This will return the Lucy and Holly entries. (We didn't have to cast the values as integer in this case because the fn:max()
function casts all values of type xs:anyAtomicType
as xs:double
; see https://www.w3.org/TR/xpath-functions-31/#func-max.)
Or, in a more compact form:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
return
$people/person[@age = max($people/person/@age)]
add a comment |
There are a few approaches one might take. Following your start to its logical conclusion, you've already ordered your items by $count
, so you can bind these ordered items to a variable and retrieve the greatest one using the fn:last()
function. You didn't provide source data, so here's a complete example demonstrating the approach:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
</people>
let $ordered :=
(
for $person in $people/person
order by $person/@age cast as xs:integer
return
$person
)
let $oldest := $ordered[last()]
return
$oldest
This will return the Lucy entry. (If we hadn't cast the values as xs:integer
we would've gotten Billy.)
However, it's possible there will be two or more people who have the same greatest age. To return all entries with the maximum age, you could first find the maximum value, then select the items with that value:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
let $ages := $people/person/@age
let $max := max($ages)
let $oldest := $people/person[@age = $max]
return
$oldest
This will return the Lucy and Holly entries. (We didn't have to cast the values as integer in this case because the fn:max()
function casts all values of type xs:anyAtomicType
as xs:double
; see https://www.w3.org/TR/xpath-functions-31/#func-max.)
Or, in a more compact form:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
return
$people/person[@age = max($people/person/@age)]
add a comment |
There are a few approaches one might take. Following your start to its logical conclusion, you've already ordered your items by $count
, so you can bind these ordered items to a variable and retrieve the greatest one using the fn:last()
function. You didn't provide source data, so here's a complete example demonstrating the approach:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
</people>
let $ordered :=
(
for $person in $people/person
order by $person/@age cast as xs:integer
return
$person
)
let $oldest := $ordered[last()]
return
$oldest
This will return the Lucy entry. (If we hadn't cast the values as xs:integer
we would've gotten Billy.)
However, it's possible there will be two or more people who have the same greatest age. To return all entries with the maximum age, you could first find the maximum value, then select the items with that value:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
let $ages := $people/person/@age
let $max := max($ages)
let $oldest := $people/person[@age = $max]
return
$oldest
This will return the Lucy and Holly entries. (We didn't have to cast the values as integer in this case because the fn:max()
function casts all values of type xs:anyAtomicType
as xs:double
; see https://www.w3.org/TR/xpath-functions-31/#func-max.)
Or, in a more compact form:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
return
$people/person[@age = max($people/person/@age)]
There are a few approaches one might take. Following your start to its logical conclusion, you've already ordered your items by $count
, so you can bind these ordered items to a variable and retrieve the greatest one using the fn:last()
function. You didn't provide source data, so here's a complete example demonstrating the approach:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
</people>
let $ordered :=
(
for $person in $people/person
order by $person/@age cast as xs:integer
return
$person
)
let $oldest := $ordered[last()]
return
$oldest
This will return the Lucy entry. (If we hadn't cast the values as xs:integer
we would've gotten Billy.)
However, it's possible there will be two or more people who have the same greatest age. To return all entries with the maximum age, you could first find the maximum value, then select the items with that value:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
let $ages := $people/person/@age
let $max := max($ages)
let $oldest := $people/person[@age = $max]
return
$oldest
This will return the Lucy and Holly entries. (We didn't have to cast the values as integer in this case because the fn:max()
function casts all values of type xs:anyAtomicType
as xs:double
; see https://www.w3.org/TR/xpath-functions-31/#func-max.)
Or, in a more compact form:
xquery version "3.1";
let $people :=
<people>
<person age="42" name="molly"/>
<person age="70" name="lucy"/>
<person age="9" name="billy"/>
<person age="70" name="holly"/>
</people>
return
$people/person[@age = max($people/person/@age)]
answered Nov 16 '18 at 19:01
joewizjoewiz
4,0571220
4,0571220
add a comment |
add a comment |
Guessing from your code, I think max
with for
should work using XPath 2.0+
max(for $country in doc("world.xml")/world/country
return count(tokenize($country/@cities, 's+')))
and then select the corresponding nodes
/world/country[count(tokenize(@cities, 's+')) = max(for $country in /world/country
return count(tokenize($country/@cities, 's+')))]
add a comment |
Guessing from your code, I think max
with for
should work using XPath 2.0+
max(for $country in doc("world.xml")/world/country
return count(tokenize($country/@cities, 's+')))
and then select the corresponding nodes
/world/country[count(tokenize(@cities, 's+')) = max(for $country in /world/country
return count(tokenize($country/@cities, 's+')))]
add a comment |
Guessing from your code, I think max
with for
should work using XPath 2.0+
max(for $country in doc("world.xml")/world/country
return count(tokenize($country/@cities, 's+')))
and then select the corresponding nodes
/world/country[count(tokenize(@cities, 's+')) = max(for $country in /world/country
return count(tokenize($country/@cities, 's+')))]
Guessing from your code, I think max
with for
should work using XPath 2.0+
max(for $country in doc("world.xml")/world/country
return count(tokenize($country/@cities, 's+')))
and then select the corresponding nodes
/world/country[count(tokenize(@cities, 's+')) = max(for $country in /world/country
return count(tokenize($country/@cities, 's+')))]
edited Nov 16 '18 at 19:18
answered Nov 16 '18 at 19:02
wp78dewp78de
10.4k62044
10.4k62044
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339333%2fcount-number-of-words-in-each-attribute%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown