Reputation: 699
I have this XML file and I need to get the name and the author/s of a book, where name of at least one author starts with "E".
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE library SYSTEM "knihy.dtd">
<library>
<book isbn="0470114878">
<author hlavni="hlavni">David Hunter</author>
<author>Jeff Rafter</author>
<author>Joe Fawcett</author>
<author>Eric van der Vlist</author>
<author>Danny Ayers</author>
<name>Beginning XML</name>
<publisher>Wrox</publisher>
</book>
<book isbn="0596004206">
<author>Erik Ray</author>
<name>Learning XML</name>
<publisher>O'Reilly</publisher>
</book>
<book isbn="0764547593">
<author>Kay Ethier</author>
<author>Alan Houser</author>
<name>XML Weekend Crash Course</name>
<year>2001</year>
</book>
<book isbn="1590596765">
<author>Sas Jacobs</author>
<name>Beginning XML with DOM and Ajax</name>
<publisher>Apress</publisher>
<year>2006</year>
</book>
</library>
I tried this approach
for $book in /library/book[starts-with(author, "E")]
return $book
but it returns XPathException in invokeTransform: A sequence of more than one item is not allowed as the first argument of starts-with() ("David Hunter", "Jeff Rafter", ...). So how can I check this sequence?
Upvotes: 2
Views: 191
Reputation: 89285
As the error message suggests, use starts-with()
in predicate for individual author
elements, instead of passing all author
child elements to starts-with()
function at once :
for $book in /library/book[author[starts-with(., "E")]]
return $book
The above will return all book
s where name of at least one of the author
starts with "E"
.
output :
<book isbn="0470114878">
<author hlavni="hlavni">David Hunter</author>
<author>Jeff Rafter</author>
<author>Joe Fawcett</author>
<author>Eric van der Vlist</author>
<author>Danny Ayers</author>
<name>Beginning XML</name>
<publisher>Wrox</publisher>
</book>
<book isbn="0596004206">
<author>Erik Ray</author>
<name>Learning XML</name>
<publisher>O'Reilly</publisher>
</book>
Upvotes: 3