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

index-of() behavior

Florent Georges lists at fgeorges.org
Thu Mar 19 15:55:14 PST 2009


  index-of() behavior
Joe Wicentowski wrote:

> <results>{
> let $content :=
>     <x>
>         <y n="1">z</y>
>         <y n="2">z</y>
>     </x>
> for $child in $content/y
> let $index := fn:index-of($content/y, $child)
> return
>     <result>{$index}</result>
> }</results>

> I expected this to return:

> <results>
>    <result>1</result>
>    <result>2</result>
> </results>

  index-of() compares items by value.  If you want to use identity comparison instead, you can write your own function to do that.  A simple recursive function is obvious:

declare function local:index-of($seq as node()*, $n as node())
      as xs:integer*
{
   local:index-of($seq, $n, 1)
};

declare function local:index-of($seq as node()*, $n as node(), $i as xs:integer)
      as xs:integer*
{
   if ( empty($seq) ) then
      ()
   else if ( $seq[1] is $n ) then
      ( $i, local:index-of(remove($seq, 1), $n, $i + 1) )
   else
      local:index-of(remove($seq, 1), $n, $i + 1)
};

<results> {
   let $content := <x>
                      <y n="1">z</y>
                      <y n="2">z</y>
                   </x>
   for $child in $content/y
   return
       <result>{ local:index-of($content/y, $child) }</result>
}
</results>

results in:

<results>
   <result>1</result>
   <result>2</result>
</results>

  Hope that helps,

-- 
Florent Georges
http://www.fgeorges.org/
























      



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.