File: functx-functx-update-attributes-2.xq

package info (click to toggle)
qtxmlpatterns-opensource-src 5.15.15-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 308,860 kB
  • sloc: xml: 360,343; cpp: 91,994; ansic: 388; sh: 53; sed: 31; makefile: 23
file content (42 lines) | stat: -rw-r--r-- 1,687 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(:**************************************************************:)
(: Test: functx-functx-update-attributes-2                                  :)
(: Written by: Priscilla Walmsley (Frans Englich is maintainer) :)
(: Date: 2008-05-16+02:00                                       :)
(:**************************************************************:)

declare namespace functx = "http://www.example.com/";
(:~
 : Updates the attribute value of an XML element 
 :
 : @author  Priscilla Walmsley, Datypic 
 : @version 1.0 
 : @see     http://www.xqueryfunctions.com/xq/functx_update-attributes.html 
 : @param   $elements the element(s) for which you wish to update the attribute 
 : @param   $attrNames the name(s) of the attribute(s) to add 
 : @param   $attrValues the value(s) of the attribute(s) to add 
 :) 
declare function functx:update-attributes 
  ( $elements as element()* ,
    $attrNames as xs:QName* ,
    $attrValues as xs:anyAtomicType* )  as element()? {
       
   for $element in $elements
   return element { node-name($element)}
                  { for $attrName at $seq in $attrNames
                    return if ($element/@*[node-name(.) = $attrName])
                           then attribute {$attrName}
                                     {$attrValues[$seq]}
                           else (),
                    $element/@*[not(node-name(.) = $attrNames)],
                    $element/node() }
 } ;

let $in-xml := <in-xml xmlns:new='http://new'>
   <a att1='def'>x</a>
   <b>x</b>
   <c new:att1='def'>x</c>
</in-xml>
return (functx:update-attributes(
   $in-xml/a,
   (xs:QName('att1'),xs:QName('att2')),
   (1,2)))