XPath 查询以解析属性中的所有 IDREFS(可能包含许多 ID) [英] XPath Query to parse all IDREFS in an attribute (containing possibly many of IDs)
问题描述
我需要提出一个查询,给出未售出商品的类型的产品意思是如果商品属于服装类型,并且没有服装商品出现在列表中交易,我需要显示它.
I need to come up with a query that gives the products of the types from which no items were sold Meaning if an item is of the type clothing, and no clothing items appear in the list of transactions, I need to display it.
这是我的 XML 文件(为超级加拿大人道歉):
This is my XML file (apologies for the super-Canadian-ness):
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE store [
<!ELEMENT store (product*, transaction*)>
<!ATTLIST store name CDATA #REQUIRED >
<!ELEMENT product EMPTY>
<!ATTLIST product
name ID #REQUIRED
type CDATA #REQUIRED
price CDATA #REQUIRED
>
<!ELEMENT transaction EMPTY>
<!ATTLIST transaction
products IDREFS #REQUIRED
sumPrice CDATA #REQUIRED
>
]>
<store name="Gordons">
<product name="beaverCoat" type="clothing" price="100"/>
<product name="hockeyStick" type="equipment" price="30"/>
<product name="hockeyPuck" type="equipment" price="5"/>
<product name="icePick" type="equipment" price="40"/>
<product name="mooseMeat" type="food" price="350"/>
<product name="salmon" type="food" price="15"/>
<transaction products="hockeyPuck hockeyStick" sumPrice="35"/>
<transaction products="hockeyStick mooseMeat" sumPrice="380"/>
<transaction products="salmon mooseMeat" sumPrice="365"/>
<transaction products="hockeyStick hockeyStick hockeyStick" sumPrice="30"/>
</store>
期望的输出
因为它是一个类别(服装)的产品,没有购买任何东西.即没有交易包括服装.
<product name="beaverCoat" type="clothing"/>
because it's a product from a category (clothing) from which nothing was bought. i.e. no transactions include clothing.
我的尝试我已经尝试了一些查询,但我就是做对了.这是我得到的最接近的:
MY ATTEMPT I've played around with some queries but I just can't get it right. This is the closest I've gotten:
//product[@type !=//transactions/@products/@type]
这似乎应该可行 - 在所有 transactions
中查找 type
不等于任何 type
的所有产品但是我遇到了很多错误.
It seems like this should work - find all the products whose type
is no equal to any of the type
in all of the transactions
however I am getting lots of errors.
如果有人能提供解决方案并提供一些解释,我将不胜感激.
I would really appreciate if someone could provide the solution with a little explanation.
推荐答案
您可以使用 id()
函数获取已售出的所有商品的节点集,在products<的节点集上使用它/code>
transaction
元素的属性:
You can use the id()
function to get the node-set of all items that have been sold, using it on the node-set of products
attributes of transaction
elements:
id(//transaction/@products)
并且您可以轻松扩展它以获取已售商品的type
:
and you can easily extend that to get the type
of items that have been sold:
id(//transaction/@products)/@type
你想要的是这个集合中type
not的所有产品,由下式给出:
What you want is all products where the type
is not in this set, which is given by:
//product[not(@type = id(//transaction/@products)/@type)]
在您的示例 XML 上使用它只会选择 beaverCoat
产品节点.
Using this on your example XML selects only beaverCoat
product node.
这篇关于XPath 查询以解析属性中的所有 IDREFS(可能包含许多 ID)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!