Xpath 选择包含特定列表子集的元素 [英] Xpath select elements that contains a subset of specific list

查看:37
本文介绍了Xpath 选择包含特定列表子集的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天!假设以下 XML:

Good day! Assume the following XML:

<store>
    <book id="b1"></book>
    <book id="b2"></book>
</store>
<store>
    <book id="b2"></book>
    <book id="b4"></book>
</store>
<booklist>
    <book id="b1"></book>
    <book id="b2"></book>
    <book id="b3"></book>
</booklist>

我想编写一个 Xpath 查询来选择哪些商店
书单里有他们所有的书.在我的示例中,它是第一家商店,但不是第二家.
我试过了
//store[./book/@id =/booklist/book/@id]
但它选择至少包含 1 本普通书的商店,而不是全部.
另外,没有找到任何像包含"这样的两个节点列表的谓词,所以我缺乏想法.

I want to write an Xpath query that will select stores which
has all their books in booklist. In my example it's first store, but not the second.
I've tried
//store[./book/@id = /booklist/book/@id]
but it chooses stores which contain at least 1 common book, and not all of them.
Also, didn't found any predicate like "contains" for two node lists, so I'm lack of ideas.

我非常感谢任何帮助,但我更喜欢使用基本"Xpath 功能(如果存在)的解决方案.这对我来说是一门新语言.不过,感谢到目前为止所有回复的人.

Im very appreciate any help, but I'd prefer a solution, which uses "basic" Xpath features (if exists). It's a new language for me. Yet, thanks for everyone who is responded so far.

推荐答案

使用:

/*/store[not(book[not(@id = /*/booklist/*/@id)])]

应用于此 XML 文档时(提供的文档,通过用单个顶部元素包装它而使其格式良好):

When applied on this XML document (the provided one, made well-formed by wrapping it with a single top element):

<t>
    <store>
        <book id="b1"></book>
        <book id="b2"></book>
    </store>
    <store>
        <book id="b2"></book>
        <book id="b4"></book>
    </store>
    <booklist>
        <book id="b1"></book>
        <book id="b2"></book>
        <book id="b3"></book>
    </booklist>
</t>

想要的stor元素被选中:

the wanted stor element is selected:

<store>
   <book id="b1"/>
   <book id="b2"/>
</store>

使用 XSLT 作为 XPath 的宿主进行验证:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/">
     <xsl:copy-of select=
     "/*/store[not(book[not(@id = /*/booklist/*/@id)])]"/>
 </xsl:template>
</xsl:stylesheet>

当此 XSLT 转换应用于上述 XML 文档时,会产生所需的正确结果:

<store>
   <book id="b1"/>
   <book id="b2"/>
</store>

说明:

表达式:

/*/store[not(book[not(@id = /*/booklist/*/@id)])]

意思是:

选择所有 store 元素(顶部元素的子元素),这样它们中甚至没有一个 book(子元素)的 id 属性的值是 booklistbook(子)的 id 属性值之一.

Select all store elements (children of the top element) such that there is not even a single book (child) in them whose id attribute's value is one of the id attributes values of the books (children) of booklist.

这篇关于Xpath 选择包含特定列表子集的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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