简体   繁体   中英

Selecting Node for every time another node is repeated with XQuery with IMPORTXML on Google Sheets

So here's what I want to do. The following is the xml info (simplified) I get from an invoicing program. I would like to select the Invoicer Name for every time I have a different product. So in my query I'd get:

Element='<invoicerName>Jack</invoicerName>'
Element='<invoicerName>Jack</invoicerName>'
Element='<invoicerName>Jack</invoicerName>'

Thank you!

<?xml version="1.0" encoding="UTF-8"?>
<invoices>
    <invoice>
    <invoicerInfo>
        <invoicerName>Jack</invoicerName>
    </invoicerInfo>
    <invoiceDetails>
        <productDescription>Soda</productDescription>
        <productDescription>Popcorn</productDescription>
        <productDescription>Tickets</productDescription>
    </invoiceDetails>
    <invoice>
</invoices>

I've tried a slew of options but I don't get repeated invoicerName values.

Google Sheets IMPORTXML seems to support XPath 2.0 so you should be able to use:

for $invoice in //invoice
return
    for $p in distinct-values($invoice//productDescription)
    return
        $invoice//invoicerName

That'll generate a list of invoicerName nodes, one for each unique productDescription of a given invoice

If Google Sheets has support for group by you can also use

for $i in //invoice
let $name := $i//invoicerName
return
  for $p in $i//productDescription
  group by $p
  return $name

here is the query in action with extended test data https://xqueryfiddle.liberty-development.net/6qVSgfb

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