xpath:contains()用于一组答案 [英] xpath: contains() for a group of answers

查看:84
本文介绍了xpath:contains()用于一组答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习XPath,但是在进行嵌套搜索(使用包含)时遇到了麻烦。

I'm trying to learn XPath, and I am having trouble with doing a nested search (using contains).

具体来说,我遇到了以下问题:

Specifically, I was given the following question:

根据以下dtd,有作者列表和书籍列表:

There is a list of authors, and a list of books, according to the following dtd:

<!ELEMENT db1 (book*, author*)>
<!ELEMENT book (title)>
<!ATTLIST book
    bid ID #REQUIRED
    authors IDREFS #REQUIRED
>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ATTLIST author
    aid ID #REQUIRED
>

编写一个XPath表达式,该表达式返回写书的作者人数。可以假设没有两个包含彼此的作者ID。

Write an XPath expression that returns the number of authors who wrote books. It is possible to assume that there are no two author ids that contain one another.

我尝试了很多事情,但始终收到错误消息:包含的项目过多。
我正在尝试运行以下命令:

I tried many things, but I keep getting an error of "Too many items in contains". I am trying to run something like this:

//author/@aid[contains(//book/@authors/string(.),  string(.))]

我正在使用以下xml文件例如:

I am using the following xml file as an example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE db1 SYSTEM "C:\blabla\db1.dtd">
<db1>
<book authors="a1 a3 a4" bid="b1">
<title>Book 1</title>
</book>
<book authors="a1 a2 a3" bid="b2">
<title>Book 2</title>
</book>
<book authors="a4" bid="b3">
<title>Book 3</title>
</book>
<author aid="a1"></author>
<author aid="a91"></author>
<author aid="a2"></author>
<author aid="a88"></author>
<author aid="a3"></author>
<author aid="a4"></author>
<author aid="a5"></author>
<author aid="a6"></author>

</db1>

预期答案应该是

a1 a2 a3 a4

有什么建议吗?

谢谢。

推荐答案

我找到了想要的答案。其实并不难,只需熟悉XPath的 id功能即可。

I found the answer I was looking for. It is not that difficult actually, it is just necessary to be familiar with the 'id' feature of XPath.

对此的XPAth查询是:
count(id(// book / @ authors))

The XPAth query for this is: count(id(//book/@authors))

作者列表可以为 id(// book / @ authors)。请注意,此xquery返回完整的xml(而非仅名称):

The list of authors could be given as id(//book/@authors). Notice that this xquery returns the full xml (and not the names only):

<author aid="a1"/>
<author aid="a2"/>
<author aid="a3"/>
<author aid="a4"/>

请参见引用

函数包含

id函数通过元素的唯一ID选择元素。当id的参数为node-set类型时,则结果是将id应用于参数node-set中每个节点的字符串值的结果的并集。当id的参数为任何其他类型时,该参数将转换为字符串,就像通过调用string函数一样;字符串被分成一个由空格分隔的令牌列表(空格是与产品S匹配的任何字符序列);结果是一个节点集,其中包含与上下文节点相同的文档中的元素,这些元素的唯一ID等于列表中的任何标记。

The id function selects elements by their unique ID. When the argument to id is of type node-set, then the result is the union of the result of applying id to the string-value of each of the nodes in the argument node-set. When the argument to id is of any other type, the argument is converted to a string as if by a call to the string function; the string is split into a whitespace-separated list of tokens (whitespace is any sequence of characters matching the production S); the result is a node-set containing the elements in the same document as the context node that have a unique ID equal to any of the tokens in the list.

这篇关于xpath:contains()用于一组答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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