简体   繁体   中英

How to count occurrences of attributes using XQuery

I am using XQuery to count occurrences of related comments, on a thread on Social Media. However, I only want to count these occurrences, if the comments are made by females. (This is for a gender related research project at Uni.

So far I have got XQuery to count all the occurrences of the comments made by females by using this:

for $t in doc ("women.xml")
let $a:=$t//comment/@gender="female"
return count ($a)

However I need some help working out how I would adapt this to account the occurrences of appearance specific comments said by females.

Thank you for your help

First, note that

let $a:=$t//comment/@gender="female" return count ($a)

always returns 1. That's because the result of "=" is a boolean, and a boolean value is a sequence of length 1. What you intended is

let $a:=$t//comment[@gender="female"] return count ($a)

or more simply

count($t//comment[@gender="female"])

Now, if you only want to define "appearance-specific" comments, you can do

count($t//comment[@gender="female"][local:is-appearance-specific(.)])

and then you need to define a function

declare function local:is-appearance-specific(
        $c as element(comment)) as xs:boolean {
   ....
};

which returns true if the comment is considered "appearance-specific".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM