使用 count() 函数计算和显示 XQuery 文档中的元素数量时出现问题
Having issues calculating and displaying the number of elements in an XQuery doc using the count() function
一直在处理我的第一个 XQuery 项目,我被指示创建 3 个简单的查询。他们中的大多数应该计算 DC 中某些犯罪的数量(在本例中为抢劫)。教科书解释的方式有点混乱(它说使用"count",如在计算违规次数),但是它说要计算元素的数量 for该罪行,因此结果文件应显示在示例 database/xml 文件中记录的时间段内发生了 1030 起抢劫案。当我 运行 查询时,"ROBBERY" 和计数都不会显示。
这是我的代码:
xquery version "1.0";
(:
Query to display the total number of incidents for
crimes specified by the user
:)
declare variable $crimeType as xs:string external;
declare variable $crimes := doc('dc_crime.xml');
<results>{
<incidents type="$crimeType">
crimeCount="{
count(doc('dc_crime.xml')//crimes[crimeType='ROBBERY'])
}">
{
doc('dc_crime.xml')//crimes[crimeType='ROBBERY']
}
</incidents>
}</results>
XML 文件的一部分:
<crimes>
<crime id="5047867">
<dateTime>2013-06-05T00:00:00</dateTime>
<month>6-Jun</month>
<day>4-Wed</day>
<offense>SEX ABUSE</offense>
<method>KNIFE</method>
<ward>4</ward>
</crime>
<crime id="7083463">
<dateTime>2013-07-08T00:00:00</dateTime>
<month>7-Jul</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="11010107">
<dateTime>2013-07-31T00:00:00</dateTime>
<month>7-Jul</month>
<day>4-Wed</day>
<offense>HOMICIDE</offense>
<method>OTHERS</method>
<ward>5</ward>
</crime>
<crime id="11250281">
<dateTime>2013-07-08T00:00:00</dateTime>
<month>7-Jul</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>7</ward>
</crime>
<crime id="12055744">
<dateTime>2013-08-19T00:00:00</dateTime>
<month>8-Aug</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="12174206">
<dateTime>2013-08-26T00:00:00</dateTime>
<month>8-Aug</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="13069723">
<dateTime>2013-07-23T00:00:00</dateTime>
<month>7-Jul</month>
<day>3-Tue</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>7</ward>
</crime>
有人可以提供任何提示吗?谢谢。
这个应该有效:
xquery version "1.0";
(:
Query to display the total number of incidents for
crimes specified by the user
:)
declare variable $crimeType as xs:string external;
declare function local:countCrimes($crimeType as xs:string) as element(results) {
let $crimes := doc('dc_crime.xml')//crime[offense = $crimeType]
let $count := count($crimes)
return
if ($count > 0 ) then
<results>
<incidents type="{$crimeType}" crimeCount="{$count}">{$crimes}</incidents>
</results>
else
<results>No crimes found for that crime type.</results>
};
local:countCrimes($crimeType)
这是正确的查询。
declare variable $crimeType as xs:string external;
declare variable $crimes := doc('dc_crime.xml')/crimes/crime;
<results>
<incidents type="{$crimeType}">
{
count
(for $c in $crimes
where $c/offense = $crimeType
return $c)
}
</incidents>
</results>
记住当你运行外部变量使用Saxon时你必须设置外部变量的值“parameter=value”,参数是外部变量的名称不要添加“$”符号,值是“变量”。
命令提示符中的 Java 命令应如下所示:
java -cp saxon9he.jar net.sf.saxon.Query dc_query1.xq crimeType=ROBBERY -o:dc_results1.xml
否则您将收到类似“没有为必需参数 crimeType 提供值
查询因动态错误而失败:如果您不包含外部变量,则没有为必需的参数 crimeType 提供任何值。
一直在处理我的第一个 XQuery 项目,我被指示创建 3 个简单的查询。他们中的大多数应该计算 DC 中某些犯罪的数量(在本例中为抢劫)。教科书解释的方式有点混乱(它说使用"count",如在计算违规次数),但是它说要计算元素的数量 for该罪行,因此结果文件应显示在示例 database/xml 文件中记录的时间段内发生了 1030 起抢劫案。当我 运行 查询时,"ROBBERY" 和计数都不会显示。
这是我的代码:
xquery version "1.0";
(:
Query to display the total number of incidents for
crimes specified by the user
:)
declare variable $crimeType as xs:string external;
declare variable $crimes := doc('dc_crime.xml');
<results>{
<incidents type="$crimeType">
crimeCount="{
count(doc('dc_crime.xml')//crimes[crimeType='ROBBERY'])
}">
{
doc('dc_crime.xml')//crimes[crimeType='ROBBERY']
}
</incidents>
}</results>
XML 文件的一部分:
<crimes>
<crime id="5047867">
<dateTime>2013-06-05T00:00:00</dateTime>
<month>6-Jun</month>
<day>4-Wed</day>
<offense>SEX ABUSE</offense>
<method>KNIFE</method>
<ward>4</ward>
</crime>
<crime id="7083463">
<dateTime>2013-07-08T00:00:00</dateTime>
<month>7-Jul</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="11010107">
<dateTime>2013-07-31T00:00:00</dateTime>
<month>7-Jul</month>
<day>4-Wed</day>
<offense>HOMICIDE</offense>
<method>OTHERS</method>
<ward>5</ward>
</crime>
<crime id="11250281">
<dateTime>2013-07-08T00:00:00</dateTime>
<month>7-Jul</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>7</ward>
</crime>
<crime id="12055744">
<dateTime>2013-08-19T00:00:00</dateTime>
<month>8-Aug</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="12174206">
<dateTime>2013-08-26T00:00:00</dateTime>
<month>8-Aug</month>
<day>2-Mon</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>1</ward>
</crime>
<crime id="13069723">
<dateTime>2013-07-23T00:00:00</dateTime>
<month>7-Jul</month>
<day>3-Tue</day>
<offense>SEX ABUSE</offense>
<method>OTHERS</method>
<ward>7</ward>
</crime>
有人可以提供任何提示吗?谢谢。
这个应该有效:
xquery version "1.0";
(:
Query to display the total number of incidents for
crimes specified by the user
:)
declare variable $crimeType as xs:string external;
declare function local:countCrimes($crimeType as xs:string) as element(results) {
let $crimes := doc('dc_crime.xml')//crime[offense = $crimeType]
let $count := count($crimes)
return
if ($count > 0 ) then
<results>
<incidents type="{$crimeType}" crimeCount="{$count}">{$crimes}</incidents>
</results>
else
<results>No crimes found for that crime type.</results>
};
local:countCrimes($crimeType)
这是正确的查询。
declare variable $crimeType as xs:string external;
declare variable $crimes := doc('dc_crime.xml')/crimes/crime;
<results>
<incidents type="{$crimeType}">
{
count
(for $c in $crimes
where $c/offense = $crimeType
return $c)
}
</incidents>
</results>
记住当你运行外部变量使用Saxon时你必须设置外部变量的值“parameter=value”,参数是外部变量的名称不要添加“$”符号,值是“变量”。 命令提示符中的 Java 命令应如下所示:
java -cp saxon9he.jar net.sf.saxon.Query dc_query1.xq crimeType=ROBBERY -o:dc_results1.xml
否则您将收到类似“没有为必需参数 crimeType 提供值 查询因动态错误而失败:如果您不包含外部变量,则没有为必需的参数 crimeType 提供任何值。