噩梦般的,有效错误:[XPTY0004] 预期单个项目,(元素 x { ... },元素 x { ... }, ...)找到 [英] Nightmarish, valid Error: [XPTY0004] Single item expected, (element x { ... }, element x { ... }, ...) found

查看:22
本文介绍了噩梦般的,有效错误:[XPTY0004] 预期单个项目,(元素 x { ... },元素 x { ... }, ...)找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我最近一次从噩梦中访问 XML 数据库的尝试中,我已经非常接近了.我实际上成功了,使用了测试数据库;但是,当我将它应用到我实际尝试访问的数据库而不是 BaseX 示例数据库时,我得到了一个特殊的错误,这是对 XML 文件的有效投诉:

通过 http://basex.org/products/live-demo/>

doc('test') :=

<item_number>1171270</item_number><卖家信息><seller_company_id>6356</seller_company_id><seller_rating>C31</seller_rating><seller_rating>T150 小时.</seller_rating></seller_info><产品信息><unit>2022</unit><sinfo>55个案例</sinfo><sinfo>游戏王集换式卡包</sinfo><sinfo>.45kg/单位</sinfo><sinfo>24.7500公斤装运</sinfo></product_info><产品信息><unit>9291</unit><sinfo>7个单位</sinfo><sinfo>火影忍者,经典,可动人偶</sinfo><sinfo>每单位1.8kg</sinfo><sinfo>12.6kg装运</sinfo></product_info></项目>

0:编写您自己的查询... :=

let $doc := doc('test')对于 $doc//item 中的 $v其中包含($v/product_info/unit,'9291')返回$v/seller_info/seller_company_id

返回:

错误:停在第 3 行,第 39 列:[XPTY0004] 预期单个项目,找到(元素单元 { ... },元素单元 { ... }).

我不能说我没想到会遇到这样的问题.不幸的是,我没有格式化 XML 文档——其他人做了——,正如你所看到的,它的格式非常糟糕.我试图访问它的部分原因是:重构它.

有没有办法运行我试图在文档上运行的查询并获得结果,而不会出现这个错误?当我尝试将 sinfo 定位为返回值时,我也在查看返回中包含 Single item expected 的错误,对吗?有没有办法获得,例如,所有的sinfo的?或者只有 每个 product_info 的第二个 sinfo 怎么样,而不会有这个讨厌的错误回吐我?

解决方案

你在 where 子句中的路径 $v/product_info/unit 每次调用将产生一个以上的项目,而 contains() 函数只接受单个项目作为参数.以下查询将为您提供预期的结果:

let $doc := doc('test')对于 $doc//item 中的 $v$v/product_info/unit 中的一些 $i 满足 contains($i,'9291')返回 $v/seller_info/seller_company_id

另一种解决方案(在谓词[...]中,每一项都将绑定到上下文项.并一一处理):

let $doc := doc('test')对于 $doc//item 中的 $v其中 $v/product_info/unit[contains(.,'9291')]返回 $v/seller_info/seller_company_id

In my most recent attempt to access the XML database from my nightmares, I've come very close. I actually succeeded, using the test database; however, when I apply it to the database I'm actually trying to access, rather than the BaseX sample database, I get a special brand of error, which is a valid complaint about the XML file:

via http://basex.org/products/live-demo/

doc('test') :=

<item>
<item_number>1171270</item_number>
<seller_info>
<seller_company_id>6356</seller_company_id>
<seller_rating>C31</seller_rating>
<seller_rating>T150 hr.</seller_rating>
</seller_info>
<product_info>
<unit>2022</unit>
<sinfo>55 cases</sinfo>
<sinfo>Yu-gi-oh trading card pack</sinfo>
<sinfo>.45kg per unit</sinfo>
<sinfo>24.7500kg shipment</sinfo>
</product_info>
<product_info>
<unit>9291</unit>
<sinfo>7 units</sinfo>
<sinfo>Naruto, Classic, action figure</sinfo>
<sinfo>1.8kg per unit</sinfo>
<sinfo>12.6kg shipment</sinfo>
</product_info>
</item>

0: write your own query... :=

let $doc := doc('test') 
for $v in $doc//item
where contains($v/product_info/unit,'9291')
return 
$v/seller_info/seller_company_id

Returns:

Error:
Stopped at line 3, column 39: [XPTY0004] Single item expected, (element unit { ... }, element unit { ... }) found.

I can't say I didn't expect to encounter a problem like this. Unfortunately, I didn't format the XML document--someone else did--, and it's all terribly formatted, as you can see. Part of the reason I'm trying to access it: to restructure it.

Is there a way to run the query I'm trying to run on the document and get results without having this error spit at me? I'm also looking at the error of having Single item expected in my returns, when I try to target sinfo as a return value, right? Is there any way to get, for example, all of the sinfo's? Or how about only the second sinfo for each product_info without having this nasty error spit back at me?

解决方案

Your path $v/product_info/unit in the where clause will yield more than one item per call, while the contains() function only accepts single items as arguments. The following query will give you the expected results:

let $doc := doc('test') 
for $v in $doc//item
where some $i in $v/product_info/unit satisfies contains($i,'9291')
return $v/seller_info/seller_company_id

Another solution (in the predicate [...], each item will be bound to the context item . and processed one by one):

let $doc := doc('test') 
for $v in $doc//item
where $v/product_info/unit[contains(.,'9291')]
return $v/seller_info/seller_company_id

这篇关于噩梦般的,有效错误:[XPTY0004] 预期单个项目,(元素 x { ... },元素 x { ... }, ...)找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆