[xquery-talk] renaming single attributes

Michael Kay mhk at mhk.me.uk
Mon Jun 20 23:00:00 PDT 2005


> Nevertheless I'm glad
> you provided this code snippet since it raises one 
> interesting question. Why
> do the match and select attributes have a unioned value of 
> "node() | @*"?
> Doesn't the first term of the union subsume the second?

node() is short for child::node() and @* is short for attribute::node(), so
together you get

   child::node() | attribute::node()

which perhaps is more intuitive...

Michael Kay


> 
>  > declare function f:identity($n as node()) {
>  >   typeswitch ($n)
>  >   case element() as $e return
>  >     element {node-name($e)} 
>  >        {$e/(@* except @att3),
>  >         if (@att3) then attribute {"NEW-att3"} 
>  > {string(@att3)} else (),
>  >         for $c in $e/node() return f:identity($c)
>  >        }
>  >   case comment() as $c return $c
>  >   case processing-instruction() as $p return $p
>  >   case text() as $t return $t
>  > }
> 
> Hmm, I'm having to go slow to parse the bit of code you've 
> got packed into
> the "case element() as $e" clause. Thanks -- seriously! -- 
> for that bit of a
> mental workout. I also hadn't seen "except" used in exactly 
> that way before.
> That's a much more natural usage than "intersect" in this particular
> instance.
> 
>  > - and you lose any namespace nodes because there's no way of 
>  > copying them.
>  > 
>  > This kind of thing would become much easier in XQuery with 
>  > higher-order functions. One could then have a standard 
>  > tree-walking function that applies a supplied function to 
>  > each node. Something like this:
>  > 
>  > declare function f:rename-att3($n as node()) {
>  >   if ($n instance of attribute(att3))
>  >   then attribute {"NEW-att3"} {string($n)}
>  >   else $n
>  > }
>  > 
>  > tree-walk(/, f:rename-att3)
> 
> That would indeed be very nice.
> 
>  > 
>  > Michael Kay
>  > http://www.saxonica.com/
>  > 
>  > 
> 
> 




More information about the talk mailing list