错误 - '/'的第一个操作数的必需项类型是node();提供的值具有项类型xs:untypedAtomic

时间:2018-05-18 10:50:11

标签: xquery

问题:查找每个发布者发布的图书总数。列出发布者和已发布的图书总数。使用publisher元素和具有count元素的图书总数包含发布商的结果。 (提示:阅读有关不同值的更多信息)

XML代码:

<catalog>
    <book id="bk103">
      <author>
        <first>Corets</first>
        <last>Eva</last>
      </author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price currency="AU$">5.95</price>
      <publisher>Apress</publisher>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
      <keywords/>
   </book>
   <book id="bk104">
      <author>
        <last>Corets</last>
        <first>Eva</first>
      </author>
      <title>Oberons Legacy</title>
      <genre>Fantasy</genre>
      <price currency="AU$">6.95</price>
      <publisher>Apress</publisher>
      <publish_date>2001-03-10</publish_date>
      <description>In post-apocalypse England, the mysterious 
      agent known only as Oberon helps to create a new life 
      for the inhabitants of London. Sequel to Maeve 
      Ascendant.</description>
      <keywords>
        <keyword>post-apocalypse</keyword>
      </keywords>
   </book>
   <book id="bk105">
      <author>
        <last>Corets</last>
        <first>Eva</first>
      </author>
      <title>The Sundered Grail</title>
      <genre>Fantasy</genre>
      <price>15.95</price>
      <publisher>Morgan Kaufman</publisher>
      <publish_date>2001-09-10</publish_date>
      <description>The two daughters of Maeve, half-sisters, 
      battle one another for control of England. Sequel to 
      Oberons Legacy.</description>
      <keywords/>
   </book>
</catalog>

我的回答:

for $b in distinct-values(//book/publisher)
let $c := count(//book[publisher = $b])
return 
<publish>
<publisher>{$b/publisher}</publisher>
<count>$c</count>
</publish>

我得到的错误: ERROR - Required item type of first operand of '/' is node(); supplied value has item type xs:untypedAtomic

2 个答案:

答案 0 :(得分:1)

分组通常会更有效率:

var window: UIWindow?


func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    return true
}

func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}

func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

请注意,for $book-group in //book group by $publisher := $book-group/publisher return <publish> <publisher>{ $publisher}</publisher> <count>{ count($book-group) }</count> </publish> 函数会将节点转换为原子值。这意味着您将使用与原始节点的关系。

答案 1 :(得分:0)

for $b in distinct-values(//book/publisher)将变量b绑定到distinct-values返回的字符串序列中的每个项目,以便输出该字符串只需使用<publisher>{$b}</publisher>。您没有任何节点可以使用/的路径。

您可能希望使用分组

for $b in catalog/book
group by $pub := $b/publisher
return 
    <publish>
      <publisher>{$pub}</publisher>
      <count>{count($b)}</count>
    </publish>

https://xqueryfiddle.liberty-development.net/bFukv8b

如果您不受XQuery版本或方言的限制,则不支持。