Wol*_*'08 0 xml xquery xml-parsing
在我最近尝试从我的恶梦中访问XML数据库时,我已经非常接近了.我实际上成功了,使用了测试数据库; 但是,当我将其应用于数据库时,我实际上是尝试访问,而不是BaseX示例数据库,我得到一个特殊的错误品牌,这是对XML文件的有效投诉:
通过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>
Run Code Online (Sandbox Code Playgroud)
0:编写自己的查询......:=
let $doc := doc('test')
for $v in $doc//item
where contains($v/product_info/unit,'9291')
return
$v/seller_info/seller_company_id
Run Code Online (Sandbox Code Playgroud)
返回:
Error:
Stopped at line 3, column 39: [XPTY0004] Single item expected, (element unit { ... }, element unit { ... }) found.
Run Code Online (Sandbox Code Playgroud)
我不能说我没想到遇到这样的问题.不幸的是,我没有格式化XML文档 - 别人做过 - 而且它的格式非常糟糕,正如你所看到的那样.我试图访问它的部分原因是:重组它.
有没有办法运行我正在尝试在文档上运行的查询并获得结果,而不会有这个错误吐在我身上?Single item expected当我尝试将目标sinfo作为返回值时,我也在查看返回错误,对吧?有没有办法获得,例如,all of the sinfo's?或者,只有the second sinfo for each product_info没有这个讨厌的错误吐回我?
您$v/product_info/unit在where子句中的路径将为每个调用生成多个项目,而该contains()函数仅接受单个项目作为参数.以下查询将为您提供预期的结果:
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
Run Code Online (Sandbox Code Playgroud)
另一个解决方案(在谓词中[...],每个项目将绑定到上下文项.并逐个处理):
let $doc := doc('test')
for $v in $doc//item
where $v/product_info/unit[contains(.,'9291')]
return $v/seller_info/seller_company_id
Run Code Online (Sandbox Code Playgroud)