Reputation:
I am trying to write an XPath query to find the locations of authors of papers which were published in 2008. I have two files, authors.xml
and papers.xml
. I have added a fragment of each file below to illustrate the structures of each.
How do I write an XPath query that gets information from both files? I have written a query to get the locations of each author, but I do not know how to extend this to limit the results to authors who published in 2008.
My current XPath query:
doc("authors.xml")//author/location
Current output (locations of all authors):
<location>Berkeley</location>
<location>Stanford</location>
Desired output (locations of only authors who published in 2008):
<location>Berkeley</location>
authors.xml
:
<authorRoot>
<author>
<first>John</first>
<last>Doe</last>
<location>Berkeley</location>
</author>
<author>
<first>Don</first>
<last>Knuth</last>
<location>Stanford</location>
</author>
</authorRoot>
papers.xml
:
<paperRoot>
<paper>
<journal>
<author>Don Knuth</author>
<title>TeX for fun</title>
<year>1995</year>
</journal>
</paper>
<paper>
<book>
<author>Jack Daniels</author>
<title>The Ballmer Peak</title>
<year>2004</year>
</book>
</paper>
<paper>
<journal>
<author>John Doe</author>
<title>P equals NP</title>
<year>2008</year>
</journal>
</paper>
</paperRoot>
Upvotes: 1
Views: 97
Reputation: 928
Well, now that we've established XPath 2.0, we can use a for statement.
for $a in doc('authors.xml')/*/author
return
if (doc('papers.xml')
/*/paper/*[author = concat($a/first, ' ', $a/last)]
/year[. = '2008'])
then $a/location else ()
This could all be on one line, but please don't do so. If whitespace is allowed, use it for readability.
Upvotes: 0