I have a small application that searches publication citations. The citations are in XML and I pass parameters to XSLT through PHP to search by fields like instrument or author. The structure looks like this:
<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>
When I call my template in the XSLT, I use a predicate to pare down which papers are shown based on the search criteria. So if the parameter $author is set, for example, I do:
<xsl:apply-templates select="paper[contains(authors/author, $author)]" />
The problem: this works for the first author in "authors", but ignores the ones after that. So in the example above, searching for "Smith" would return this paper, but "Doe" returns nothing.
How can I format my expression to take all "author" elements into account?
The contains
function expects a string as its first argument, not a set of nodes. With your expression the first result of authors/author
is converted to a string and then passed to the function.
Instead, use a predicate that tests each of the author
nodes independently:
<xsl:apply-templates select="paper[authors/author[contains(., $author)]]" />
See, for example, this explanation of Saxon's behavior:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With