我有一个小应用程序搜索出版物引用.引用是XML格式,我通过PHP将参数传递给XSLT,以便按照乐器或作者等字段进行搜索.结构如下所示:
<publications>
<paper>
<authors>
<author>James Smith</author>
<author>Jane Doe</author>
</authors>
<year>2010</year>
<title>Paper 1</title>
(more children)
</paper>
(more papers)
</publications>
Run Code Online (Sandbox Code Playgroud)
当我在XSLT中调用我的模板时,我使用谓词来减少根据搜索条件显示哪些文件.因此,如果设置参数$ author,例如,我这样做:
<xsl:apply-templates select="paper[contains(authors/author, $author)]" />
Run Code Online (Sandbox Code Playgroud)
问题:这适用于"作者"中的第一作者,但忽略了之后的作者.所以在上面的例子中,搜索"Smith"将返回本文,但"Doe"不会返回任何内容.
如何设置表达式以考虑所有 "作者"元素?
该contains函数需要一个字符串作为其第一个参数,而不是一组节点.使用表达式,第一个结果将authors/author转换为字符串,然后传递给函数.
相反,使用一个author独立测试每个节点的谓词:
<xsl:apply-templates select="paper[authors/author[contains(., $author)]]" />
Run Code Online (Sandbox Code Playgroud)
例如,请参阅撒克逊人行为的这种解释: