使用count()函数计算和显示XQuery doc中的元素数时出现问题

时间:2015-12-01 05:42:30

标签: xml xquery

一直致力于我的第一个XQuery项目,并且我被指示创建3个简单查询。他们中的大多数人应该计算DC中某些罪行的数量(在这种情况下,抢劫)。教科书解释它的方式有点令人困惑(它说使用"计数",就像计算犯罪数量一样),但它说要计算元素的数量该攻击,因此结果文档应显示在示例数据库/ 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>

有人可以提供任何提示吗?谢谢。

2 个答案:

答案 0 :(得分:1)

这个应该有效:

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)

答案 1 :(得分:0)

This is the correct query.

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>

Remember when you run external variable using Saxon you have to set the value of external variable "parameter=value", parameter is the name of the external variable don't add the "$" symbol, value is the "variable". Your Java command in command prompt should look like:

java -cp saxon9he.jar net.sf.saxon.Query dc_query1.xq crimeType=ROBBERY -o:dc_results1.xml

or you will get error like "No value supplied for required parameter crimeType Query failed with dynamic error: No value supplied for required parameter crimeType" if you don't include the external variable.