简体   繁体   English

如何使用XQuery计数属性的出现

[英]How to count occurrences of attributes using XQuery

I am using XQuery to count occurrences of related comments, on a thread on Social Media. 我在社交媒体上的一个线程上使用XQuery来计数相关评论的出现。 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. (这是针对Uni的性别相关研究项目。

So far I have got XQuery to count all the occurrences of the comments made by females by using this: 到目前为止,我已经使用XQuery来计算女性使用此方法发表的所有评论:

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 总是返回1。这是因为“ =”的结果是一个布尔值,而布尔值是一个长度为1的序列。

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". 如果评论被视为“外观特定”,则返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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