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

Selecting everything before and after path, but without duplicated leafs

J. Zhang zhangjunte at gmail.com
Mon Feb 11 17:38:42 PST 2008


  Selecting everything before and after path
On Feb 11, 2008 4:13 PM, Michael Kay <http://x-query.com/mailman/listinfo/talk> wrote:
> I think the best way to handle this is to declare either a global variable
> or a second parameter to the processNode function, initialized to the value
> of this path expression, and then replace the test for element(c01) by a
> test against this instance:
>
>   declare function local:processNode($e as node(), $selected as element())
> as node()* {
>    if ($e is $selected) then
>       return <SELECT>{$e}</SELECT>
>    else typeswitch ($e)
> >    case element()
> >       return
> >          element {name($e)} {
> >             for $c in $e/child::node() return
> >                local:processNode($c, $selected)
> >          }
> >    default return $e
> > };
> >
> > local:processNode(doc('file.xml')/*, /ead[1]/archdesc[1]/dsc[1]/c01[1])
>
> This is assuming you only want to select a single node. If you want to mark
> all nodes that match some kind of pattern, then you'll have to implement
> something akin to XSLTs pattern matching yourself. The natural solution
> would be to use the above function and pass in a second parameter which is a
> call-back function that determines whether the node matches or not;
> unfortunately however XQuery 1.0 doesn't allow higher-order functions so
> this isn't possible.


Selecting a single node is what I need. When I use your function, I am
getting this error:

XPST0003: XQuery syntax error in #...elected) then return <SELECT>{#:
    expected "else", found ">"
Static error(s) in query

I don't see any mistake in the syntax. The error is probably generated
by the "if" statement, I assume it can take a "return"?

This is what I have copied from you:

declare function local:processNode($e as node(), $selected as
element()) as node()* {
  if ($e is $selected) then return <SELECT>{ $e }</SELECT>
  else
    typeswitch ($e)
    case element()
          return
          element {name($e)} {
             for $c in $e/child::node()
                return
                   local:processNode($c, $selected)
          }
    default return $e
};

local:processNode(doc('/var/www/html/data/iisg/ead_10748500.xml')/*,
/ead[1]/archdesc[1]/dsc[1]/c01[1])


Thanks,

jz

>
> 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.