home contribute faq download

FunctX XQuery Functions

functx:copy-attributes

Copies attributes from one element to another

Google
Webxqueryfunctions.com

Description

The functx:copy-attributes function copies attributes from the element in $copyFrom to the element in $copyTo. It leaves all the original attributes and content of $copyTo, except that if $copyTo already has an attribute with the same name of one of $copyFrom's attributes, its value is updated to match $copyFrom.

Note: this function is intended to change the way elements and attributes appear in the results of a query, not to update them in an XML database. To update your XML database, you should use the implementation-specific update functions of your processor. (There is no standard XQuery update syntax yet.)

Arguments and Return Type

NameTypeDescription
$copyTo element() the element to copy attributes to
$copyFrom element() the element to copy attributes from
return value element()

XQuery Function Declaration

See XSLT definition.
XQuery Syntax for April 2005 - January 2007 (1.0):
declare namespace functx = "http://www.functx.com"; 
declare function functx:copy-attributes 
  ( $copyTo as element() ,
    $copyFrom as element() )  as element() {
       
   element { node-name($copyTo)}
           { $copyTo/@*[not(node-name(.) = $copyFrom/@*/node-name(.))],
             $copyFrom/@*,
             $copyTo/node() }

 } ;
XQuery Syntax for July 2004:
declare namespace functx = "http://www.functx.com"; 
declare function functx:copy-attributes 
  ( $copyTo as element() ,
    $copyFrom as element() )  as element() {
       
   element { node-name($copyTo)}
             { $copyTo/@*
                 [not(node-name(.) = (for $n in $copyFrom/@*
                                  return node-name($n)))],
               $copyFrom/@*,
               $copyTo/node() }

 } ;
XQuery Syntax for May 2003:
declare namespace functx = "http://www.functx.com" 
define function copy-attributes 
  ( $copyTo as element() ,
    $copyFrom as element() )  as element() {
       
   element { fn:node-name($copyTo)}
             { $copyTo/@*
                 [fn:not(fn:node-name(.) = (for $n in $copyFrom/@*
                                  return fn:node-name($n)))],
               $copyFrom/@*,
               $copyTo/node() }

 }

Examples

let $in-xml :=
<in-xml>
   <a>123</a>>
   <b x="1" y="2">456</b>
   <c x="9">123</c>
   <d z="5">123</d>
</in-xml>
return
XQuery ExampleResults
functx:copy-attributes(
   $in-xml/a, $in-xml/b)
<a x="1" y="2">123</a>
functx:copy-attributes(
   $in-xml/b, $in-xml/c)
<b x="9" y="2">456</b>
functx:copy-attributes(
   $in-xml/d, $in-xml/c)
<d z="5" x="9">123</d>

See Also

functx:add-attributesAdds attributes to XML elements
functx:add-or-update-attributesAdds attributes to XML elements
functx:update-attributesUpdates the attribute value of an XML element
functx:remove-attributesRemoves attributes from an XML element, based on name

History

Published OnLast UpdatedContributor(s)
2006-06-272007-02-26Priscilla Walmsley, Datypic, pwalmsley@datypic.com, http://www.datypic.com
Need XQuery Help?
D A T Y P I C
Training | Consulting | Development

XQuery by Priscilla WalmsleyGet the book!
XQuery by Priscilla Walmsley