[xquery-talk] trying to retrieve element value

Michael Dyck jmdyck at ibiblio.org
Wed Jan 6 12:09:28 PST 2016

Hi Mary,

In your queries, the where clause
     where /samhsa-doc//article-title[@id="AVD210_art1"]
means (roughly):
     where some document in the collection has
     an <article-title> with id="AVD210_art1"

Apparently, there *is* such a document in your collection,
so the where-condition is always true, *regardless* of what's
been bound to $mlid in the 'for' clause. That is, your second query is 
effectively equivalent to:
     for $mlid in /samhsa-doc
     where true()
     return $mlid//system-pub-id
or just:
     for $mlid in /samhsa-doc
     return $mlid//system-pub-id
which is why you're getting a result item for each document in the database.

Instead, you want to make the 'where' condition examine the document that's 
currently in hand, i.e. the one bound to $mlid in the 'for' clause. In your 
second query, in the where clause, just change "/samhsa-doc" to "$mlid", and 
I think it'll work:

     for $mlid in /samhsa-doc
     where $mlid//article-title[@id='AVD210_art1']
     return $mlid//system-pub-id

(Incidentally, since this is a fairly simple use of for-where-return, you 
could replace it with just a path expression, e.g.
would work, I think.)


More information about the talk mailing list