简体   繁体   中英

using xpath to get `for $a in distinct-values` sub elements in xquery

Simple example:

sample.xml

<customers>
  <customer>
    <name>Tony</name>
    <order>Burger</order>
    <id>1234</id>
  </customer>
  <customer>
    <name>Kate</name>
    <order>Soda</order>
    <id>5678</id>
  </customer>
</customers

with the XQuery

for $a in distinct-values(doc("sample.xml")/customers/customer)
return
$y

will return

Tony
Burger
1234

Kate
Soda
5678

However if I do:

for $a in distinct-values(doc("sample.xml")/customers/customer)
return
$y/name

To only return the names, it throws me an error. Can anyone explain why? I want to access certain values instead of the whole tree. Same thing if I try to do anything with $y/(something) like use it in a where expression.

Update: I figured out how to somewhat bypass my overall problem by using distinct-values() as a return expression however I'm still confused why I'm not allowed to access sub elements in distinct-values for loops

distinct-values() will convert the input into atomic values. This means that only the content will be returned and thus the xml representation will be lost.

If you really want to do this you could maybe use something like the custom function: functx:distinct-deep: http://www.xqueryfunctions.com/xq/functx_distinct-deep.html

But keep in mind that you don't want to do this in most cases, it's also likely pretty bad for performance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM