8 Replies Latest reply: Jul 28, 2011 10:52 AM by 775682 RSS

    adding attributes to a node

    775682
      Hi,

      I would like to add attributes to a node with an xquery. I've found (not easily) that I can use the following query to add one attribute :

      " insert node attribute id {"10"} into collection()/root/parent/child "

      However I can't find a way to add more than one attribute. Is there a way to update an attribute if it exists, or add it if not ?

      thanks
        • 1. Re: adding attributes to a node
          655560
          Hi,

          Here is an example:
          let $value := "10"
          for $child in collection()/root/parent/child
            let $attr := $child/@id
            return
              if (fn:exists($attr)) then (
                replace value of node $attr with $value
              ) else (
                insert node attribute id {$value} into $child
              )
          Best regards,
          Rucong Zhao
          Oracle Berkeley DB XML
          • 2. Re: adding attributes to a node
            775682
            let $value := "10"
            for $child in collection()/root/parent/child
            let $attr := $child/@id
            return
            if (fn:exists($attr)) then (
            replace value of node $attr with $value
            ) else (
            insert node attribute id {$value} into $child
            )
            So flwor is the solution :) , thanks. I was thinking about a replace node attribute command or something like that. Can you give me a link to find documentation about the insert node attribute syntax. I can't find one :(
            I've found it just by being lucky

            Edited by: cscetbon on 25 juil. 2011 00:44
            • 3. Re: adding attributes to a node
              655560
              Hi,

              Here is a website for XQuery functions/examples. Hope that it would help:
              http://www.xqueryfunctions.com/xq/

              XQuery Update for the impatient:
              http://www.xmlmind.com/tutorials/XQueryUpdate/index.html

              Best regards,
              Rucong Zhao
              Oracle Berkeley DB XML
              • 4. Re: adding attributes to a node
                775682
                thank you Rucong, the second one seems to be good.

                Any way to add more than one attribute in one request ? Or do I need to use 2 insert node attribute ?

                Edited by: cscetbon on 25 juil. 2011 01:11
                • 5. Re: adding attributes to a node
                  655560
                  Hi,

                  Insert multi attributes with one insertion:
                  insert node (attribute id1 {$value1}, attribute id2 {$value2}) into $child
                  Best regards,
                  Rucong Zhao
                  Oracle Berkeley DB XML
                  • 6. Re: adding attributes to a node
                    775682
                    waouuuu, that's exactly what I was looking for. But where did you find this syntax ? another link ?

                    thanks
                    • 7. Re: adding attributes to a node
                      655560
                      Hi,

                      It's just a regular XQuery sequence usage. Please refer to XQuery handbooks.

                      Best reagrds,
                      Rucong Zhao
                      Oracle Berkeley DB XML
                      • 8. Re: adding attributes to a node
                        775682
                        Rucong Zhao wrote:
                        It's just a regular XQuery sequence usage. Please refer to XQuery handbooks.
                        I've one from Oreilly (Priscilla Walmsley) but no information on this sequence. Do you have one that you can recommand me ?

                        thanks