Xml XPath查询:查找与属性具有相同ID列表的元素
我需要提出一个查询,给出没有销售商品的类型的产品,这意味着如果商品属于服装类型,并且交易列表中没有出现服装商品,我需要显示它 这是我的XML文件(为超级加拿大式的错误道歉):Xml XPath查询:查找与属性具有相同ID列表的元素,xml,xpath,xpathquery,Xml,Xpath,Xpathquery,我需要提出一个查询,给出没有销售商品的类型的产品,这意味着如果商品属于服装类型,并且交易列表中没有出现服装商品,我需要显示它 这是我的XML文件(为超级加拿大式的错误道歉): 这似乎应该可以工作-查找其产品都与其他事务的产品属性匹配的所有事务。但是它没有得到任何点击。编辑:出于某种原因,我认为这是一个XSLT问题。这里有一种只使用XPath的方法: //transaction[(@products = preceding-sibling::transaction/@products or
这似乎应该可以工作-查找其产品都与其他事务的产品属性匹配的所有事务。但是它没有得到任何点击。编辑:出于某种原因,我认为这是一个XSLT问题。这里有一种只使用XPath的方法:
//transaction[(@products = preceding-sibling::transaction/@products or
@products = following-sibling::transaction/@products)]
以下是如何查询所有这些不同元素(需要XPath 2.0):
这里有一种使用XSLT的方法:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="kTrans" match="transaction" use="@products" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<n>
<xsl:apply-templates select="transaction[key('kTrans', @products)[2]]" />
</n>
</xsl:template>
</xsl:stylesheet>
在示例输入上运行时,结果是:
<n>
<transaction products="salmon mooseMeat" sumPrice="365" />
<transaction products="salmon mooseMeat" sumPrice="300" />
</n>
请注意,我将结果包装在一个
n
元素中,因为包含多个根元素的XML是无效的。有效!谢谢但是,是否有一种方法只返回不同的元素?如果sumPrice
相同,则不应同时打印这两个文件。您知道您的XPath引擎是否可以使用XPath 2.0吗?我可以想出一种在XPath2.0中排除重复项的方法,但在1.0中没有。您能看看我问的一个类似问题吗?
//transaction[(@products = preceding-sibling::transaction/@products or
@products = following-sibling::transaction/@products)]
//transaction[(@products = preceding-sibling::transaction/@products or
@products = following-sibling::transaction/@products) and
not(concat(@products, '+', @sumPrice) =
preceding-sibling::transaction/concat(@products, '+', @sumPrice))]
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="kTrans" match="transaction" use="@products" />
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/*">
<n>
<xsl:apply-templates select="transaction[key('kTrans', @products)[2]]" />
</n>
</xsl:template>
</xsl:stylesheet>
<n>
<transaction products="salmon mooseMeat" sumPrice="365" />
<transaction products="salmon mooseMeat" sumPrice="300" />
</n>