[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

Function and Query Evaluation with No XML Tags Error

Wei, Alice J. ajwei at indiana.edu
Fri Feb 29 10:03:14 PST 2008


  Function and Query Evaluation with No XML Tags Error
________________________________________
From: Michael Kay [http://x-query.com/mailman/listinfo/talk]
Sent: Friday, February 29, 2008 8:57 AM
To: Wei, Alice J.; 'Kevin Grover'
Cc: http://x-query.com/mailman/listinfo/talk
Subject: RE:  Function and Query Evaluation with No XML Tags Error

I've rather lost track of the requirement. I think you can do

declare function local:unique-nodes-by-value($seq as element()*) as
element()* {
  for $d in distinct-values($seq)
  return $seq[. = $d][1]
};

   Are you suggesting me to forget about "order by"?

That is, for each distinct value return the first element having that value.

    I think one of the issues with the data is that some have attributes of @type that says main and some say sub while others may have neither while it is ranking. The code I tried above is still parsing elements in the order they appear and not their actual "order."

  It may not be the problem, but to illustrate this issue, I have a short XML clip below:

XML:

<ad>
<head type ="main"> A Free Ticket to your Favorite Pro Sports Game in Every Sugar Daddy Pop! </head>
<head type ="sub">Basketball! Hockey! Lacrosse! Soccer! Pro track and more! </head>
<!--More elements-->
</ad>

XQuery:

declare variable $data := doc("1.xml")//ad;

declare variable $s := $data/child::head[contains(upper-case(.), 'BASKET')];

declare function local:unique-nodes-by-value($seq as element()*) as element()*
{
for $d in distinct-values($seq)
 return <ad>{$seq[. = $d][1]}</ad>
};

local:unique-nodes-by-value($s)

Current Output:

<ad>
      <head type="sub">Basketball! Hockey! Lacrosse! Soccer! Pro track and more! </head>
   </ad>

Intended Output:

<ad>
<head type ="main"> A Free Ticket to your Favorite Pro Sports Game in Every Sugar Daddy Pop! </head>
<head type ="sub">Basketball! Hockey! Lacrosse! Soccer! Pro track and more! </head>
</ad>

Thanks again.

Michael Kay
http://www.saxonica.com/


> -----Original Message-----
> From: Wei, Alice J. [mailto:http://x-query.com/mailman/listinfo/talk]
> Sent: 29 February 2008 13:47
> To: Michael Kay; 'Kevin Grover'
> Cc: http://x-query.com/mailman/listinfo/talk
> Subject: RE:  Function and Query Evaluation with
> No XML Tags Error
>
> Hi, Michael:
>
>   I found the method Kevin provided even in the output his
> extracted still not even ranked, so I guess maybe going back
> to distinct-values() may be the way to go.
>
>   Is this inside the function itself?
>
> Here is the XQuery:
>
> declare variable $data := doc("1.xml")//ad;
>
> declare variable $s := $data//head[contains(upper-case(.), 'BOOK')];
>
> declare function local:unique-nodes-by-value($seq as
> element()*) as element()* {
> $seq[not(string(.)=preceding-sibling::*/string(.))]
> };
>
> local:unique-nodes-by-value($s)
>
> Output:
>
> <?xml version="1.0" encoding="UTF-8"?>
>    <head type="sub">Comic Book Back issues</head>
>    <head type="sub">We Buy and Sell Comic Books</head>
>    <head type="sub">FREE BOOK</head>
>    <head type="sub"> GIANT NEW COMIC BOOK CATALOG #6</head>
>    <head type="main"> Selling - COMIC BOOKS -Buying <lb/>
> $100,000 Inventory<lb/>
>                         Marvel, D.C., Golden Age </head>
>    <head type="main">COMIC Books</head>
>    <head>COMIC BOOKS Wanted</head>
>    <head type="main">OLD Comic Books!</head>
>    <head>Old Comic Books</head>
>
> ======================================================
> Alice Wei
> MIS 2008
> School of Library and Information Science Indiana University
> Bloomington http://x-query.com/mailman/listinfo/talk ________________________________________
> From: Michael Kay [http://x-query.com/mailman/listinfo/talk]
> Sent: Friday, February 29, 2008 4:30 AM
> To: 'Michael Kay'; 'Kevin Grover'; Wei, Alice J.
> Cc: http://x-query.com/mailman/listinfo/talk
> Subject: RE:  Function and Query Evaluation with
> No XML Tags Error
>
> > Assuming that the typed value is the same as the string
> value, you can
> > write
> >
> > $seq[not(. = preceding-sibling::*)]
> >
> > If you really need the string value, it's
> >
> > $seq[not(./string() = preceding-sibling::*/string())]
> >
> > But using distinct-values() is likely to be a lot more efficient.
> >
>
> Actually, I failed to spot another error here. The author of
> the function has no way of knowing that the nodes in $seq
> will be siblings of each other.
> Therefore, using preceding-sibling to eliminate duplicates is
> not just inefficient, it is plain wrong.
>
> Michael Kay
> http://www.saxonica.com/
>




Purchase Stylus Studio Online Today!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2007 All Rights Reserved.