Discussion:
[xquery-talk] trying to retrieve element value
Mary McRae
2016-01-06 18:28:35 UTC
Permalink
Sorry for what is probably a very basic question; I've read through a number of XQuery tutorials but haven't been able to come up with something that yields the expected results. I have a MarkLogic database, which contains a few thousand XML documents. I'm trying to return the value of the ML UID (captured in the XML document as <system-pub-id>) for a known document identifier <article-title id="value">.

I've tried several variants of the following, but each returns the full set of system-pub-ids rather than the one matching the specified id value.

I've tried running through oXygen as well as the MarkLogic console; I get the same results (which I suppose is a good thing). I'm pretty certain that I'm missing something obvious, and appreciate any pointers.

I started with this:
xquery version "1.0-ml";
for $mlid in /samhsa-doc//system-pub-id
where /samhsa-doc//article-title[@id="AVD210_art1"]
return $mlid

but when I got the full list back (some 14,000 items - many of which don't have an article-title) I thought I'd try starting at the root ...

XQuery:
xquery version "1.0-ml";
for $mlid in /samhsa-doc
where /samhsa-doc//article-title[@id='AVD210_art1']
return $mlid//system-pub-id


XML:
<?xml version="1.0" encoding="UTF-8"?>
<samhsa-doc>
<doc-meta>
<status>Archived</status>
<system-pub-id>100001</system-pub-id>
<display-mode>Internal and External</display-mode>
</doc-meta>
<doc-content>
<samhsapub>
<article>
<front>
<article-meta>
<title-group>
<article-title id="AVD210_art1"> ... </article-title>
...


Thank you in advance,

Mary


Mary McRae, PMPĀ | XML Strategist / Technical Project ManagerĀ | ***@iqsolutions.com



_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Patrick Durusau
2016-01-06 19:38:22 UTC
Permalink
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Mary,

Try moving the extended predicate to your for statement:

for $mlid in doc("YOUR-File-Name")//article-title[@id="AVD210_art1"]
return $mlid

(untested)

Hope you are having a great day!

Patrick
Post by Mary McRae
Sorry for what is probably a very basic question; I've read through
a number of XQuery tutorials but haven't been able to come up with
something that yields the expected results. I have a MarkLogic
database, which contains a few thousand XML documents. I'm trying
to return the value of the ML UID (captured in the XML document as
<system-pub-id>) for a known document identifier <article-title
id="value">.
I've tried several variants of the following, but each returns the
full set of system-pub-ids rather than the one matching the
specified id value.
I've tried running through oXygen as well as the MarkLogic console;
I get the same results (which I suppose is a good thing). I'm
pretty certain that I'm missing something obvious, and appreciate
any pointers.
I started with this: xquery version "1.0-ml"; for $mlid in
/samhsa-doc//system-pub-id where
but when I got the full list back (some 14,000 items - many of
which don't have an article-title) I thought I'd try starting at
the root ...
XQuery: xquery version "1.0-ml"; for $mlid in /samhsa-doc where
$mlid//system-pub-id
XML: <?xml version="1.0" encoding="UTF-8"?> <samhsa-doc>
<doc-meta> <status>Archived</status>
<system-pub-id>100001</system-pub-id> <display-mode>Internal and
External</display-mode> </doc-meta> <doc-content> <samhsapub>
<article> <front> <article-meta> <title-group> <article-title
id="AVD210_art1"> ... </article-title> ...
Thank you in advance,
Mary
Mary McRae, PMP | XML Strategist / Technical Project Manager |
http://x-query.com/mailman/listinfo/talk
- --
Patrick Durusau
***@durusau.net
Technical Advisory Board, OASIS (TAB)
OpenDocument Format TC (OASIS), Project Editor ISO/IEC 26300
Co-Editor 13250-5 (Topic Maps)

Another Word For It (blog): http://tm.durusau.net
Homepage: http://www.durusau.net
Twitter: patrickDurusau

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJWjW0tAAoJEFPGsgi3MgycOVoP+wa3Y7OJeD5HNguP1cGjYH4P
sTJSnvuKjSjksxne6bnZSvcDk+NydF2e20nWArC3isy7qh00ubc7M1qrObAM2kk+
trbrads1ZaDMO8CtZCVTLgKkxC0FsM4eU0McifIKO6gyeh2HnHjgwREfUwKiPTa4
HygJFscHJ63X4VOmrRy9KLBYhOK58N8MyYStGcFz0RMsMQgFWyOwKpqzFVGPOFMM
Z6OFuYx73QrvQZjvgG0ZfKBwguoytLL5VIX7wg9L8Xn5sUCRSV0a8aReDZfCtg+w
wmzt/UFDMn34x3Lv84wZ6PoUFmkWGKgZJ6A6nYbYQCKjHzkbPaW38ccTE8uz+mPL
ZxpViZrABgZ9c3weWPxUHO48GvUJMw41xoiuLSLpUdtJ1xtaftXXJIyT6NtlD8mU
lUWRr3QhbLl1/zVbkSq9GQblXp732e3GsWkHW2ti4hlehpn77vk52Q20LhqE6CkG
T/aOHszVRHuChKzrKqehY0HPAXJfRKvxPlloeVA2U4hAH+4ISGGtNdjRHki1ADXR
pdBlywLPWc/DZpJcrwhVJLOxmKPmbmoF2GCRvyP4FEWZrxTKQIw1vGvkCP8Pvce4
fIRxZLkDXgWomuc99BqiUHUOuL7uxouLf+vuVUauGVCwJCGrRarLRRlXxbj1oanq
MJ/Q7xa0NrHdey/QxuPn
=6xE8
-----END PGP SIGNATURE-----
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk
Michael Dyck
2016-01-06 20:09:28 UTC
Permalink
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.
/samhsa-doc[.//article-title[@id='AVD210_art1']]//system-pub-id
would work, I think.)

-Michael
_______________________________________________
***@x-query.com
http://x-query.com/mailman/listinfo/talk

Loading...