对结果进行分组的 XQuery 语句
我有一个 XML 结构,需要使用 XQuery 进行查询。 据我所知,这甚至可能是不可能的,但由于我对 XQuery 很陌生,我想我可能会问。
我需要查询下面的 XML 并返回如下所示的内容:
<product>
<title>Acer Liquid</title>
<image>ACER-LIQUID.jpg</image>
<networks>
<network>O2O</network>
<network>VOD</network>
</networks>
<colours>
<colour>Blue</colour>
<colour>Black</colour>
</colours>
</product>
所以我的查询本质上是这样的: 获取所有产品详细信息,其中存在属于指向它的“每月付款”的销售包(通过 main_product),并附加指向它的每个销售包的网络属性。但按product.product_model 字段对结果进行分组。
源 XML:
<root>
<package>
<title>package1</title>
<categories><category group="Other" name="Pay Monthly"/></categories>
<properties>
<value key="main_product">PRODUCT#14649766#PART#ACERLIQUIDBLACK</value>
<value key="network_code">O2O</value>
</properties>
</package>
<package>
<title>package2</title>
<categories><category group="Other" name="Pay Monthly"/></categories>
<properties>
<value key="main_product">PRODUCT#14649700#PART#ACERLIQUIDBLUE</value>
<value key="network_code">VOD</value>
</properties>
</package>
<product>
<title>Acer Liquid</title>
<properties>
<value key="product_code">PRODUCT#14649700#PART#ACERLIQUIDBLUE</value>
<value key="product_model">Acer Liquid|ACER_LIQUID</value>
<value key="product_image_url">ACER-LIQUID.jpg</value>
<value key="colour">Blue</value>
</properties>
</product>
<product>
<title>Acer Liquid</title>
<properties>
<value key="product_code">PRODUCT#14649766#PART#ACERLIQUIDBLACK</value>
<value key="product_model">Acer Liquid|ACER_LIQUID</value>
<value key="product_image_url">ACER-LIQUID.jpg</value>
<value key="colour">Black</value>
</properties>
</product>
</root>
I have an XML structure that I need to query using XQuery.
As far as I have been able to find, this may not even be possible, but since I am so new to XQuery I thought I may ask.
I need to query the XML below and return something that looks like:
<product>
<title>Acer Liquid</title>
<image>ACER-LIQUID.jpg</image>
<networks>
<network>O2O</network>
<network>VOD</network>
</networks>
<colours>
<colour>Blue</colour>
<colour>Black</colour>
</colours>
</product>
So my query is essentially something like:
Get all product details where there is a salespackage that belongs to "Pay Monthly" that points to it (via main_product), and attach the network property from each sales package that points to it. But group the results by the product.product_model field.
Source XML:
<root>
<package>
<title>package1</title>
<categories><category group="Other" name="Pay Monthly"/></categories>
<properties>
<value key="main_product">PRODUCT#14649766#PART#ACERLIQUIDBLACK</value>
<value key="network_code">O2O</value>
</properties>
</package>
<package>
<title>package2</title>
<categories><category group="Other" name="Pay Monthly"/></categories>
<properties>
<value key="main_product">PRODUCT#14649700#PART#ACERLIQUIDBLUE</value>
<value key="network_code">VOD</value>
</properties>
</package>
<product>
<title>Acer Liquid</title>
<properties>
<value key="product_code">PRODUCT#14649700#PART#ACERLIQUIDBLUE</value>
<value key="product_model">Acer Liquid|ACER_LIQUID</value>
<value key="product_image_url">ACER-LIQUID.jpg</value>
<value key="colour">Blue</value>
</properties>
</product>
<product>
<title>Acer Liquid</title>
<properties>
<value key="product_code">PRODUCT#14649766#PART#ACERLIQUIDBLACK</value>
<value key="product_model">Acer Liquid|ACER_LIQUID</value>
<value key="product_image_url">ACER-LIQUID.jpg</value>
<value key="colour">Black</value>
</properties>
</product>
</root>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
XQuery 1.0 相当复杂的查询:
Quite complex query for XQuery 1.0:
此 XQuery:
输出:
注意:XPath 2.0 分组表达式
$vSeq[index-of($vSeq,.)[1]]
This XQuery:
Output:
Note: XPath 2.0 grouping expression
$vSeq[index-of($vSeq,.)[1]]