4 Replies Latest reply: Feb 18, 2012 2:09 PM by NicePseronMahou RSS

    Insertion of nodes using XQuery

    NicePseronMahou
      Hi everybody,
      I am using XQuery Update Facility Language for updating XML documents,
      For insertion of new nodes under existing ones, i use the primitive insert node into target+ but the XPath expression target+ must be evaluated to only single node; otherwise a dynamic error is raised.
      My question is : in what purpose this condition is imposed ? and how can i insert new node under different nodes ?.

      Thank you for advance.
        • 1. Re: Insertion of nodes using XQuery
          odie_63
          Hi,
          My question is : in what purpose this condition is imposed ? and how can i insert new node under different nodes ?.
          To process a set of nodes in XQuery, the general approach is to use a sequence iterator (a for expression).
          The same applies here with XQUF and the insert primitive.

          For example :
          copy $d := <root><item/><item/></root>
          modify (
           for $i in $d/item
           return insert node <test/> into $i
          )
          return $d
          will return :
          <root>
            <item>
              <test/>
            </item>
            <item>
              <test/>
            </item>
          </root>
          • 2. Re: Insertion of nodes using XQuery
            NicePseronMahou
            Thank you very much,
            I would be very grateful if you help me to know the goal of this condition+ because it is not imposed in other languages like XUpdate*.
            This is because i have defined a basic update language based on indexation of XML data, in order to satisfy some properties and make them easy to implement,
            and i am going to translate each XQuery update to my language, but i do not know if i should preserve this condition or not.

            Best regards.
            • 3. Re: Insertion of nodes using XQuery
              odie_63
              I would be very grateful if you help me to know the goal of this condition+ because it is not imposed in other languages like XUpdate*.
              Short answer : because that's what the specification says... :)

              OK, well I guess that's because primarily XQUF is designed to work with XML databases.
              If I use an analogy with relational databases and SQL language :

              - a SQL INSERT statement only targets a single table, so does the XQUF insert primitive by targeting only a single node per expression
              - a SQL DELETE statement can target multiple rows, the delete primitive too.

              XQUF is an extension of XQuery, and as such it's meant to combine with the base language.
              From a certain point of view, as I said earlier, it's not true to say that XQUF's insert primitive can't process multiple targets, because to do so we just have to use the base language : a FLWOR expression.

              If XUpdate does not impose this restriction then I suppose, one way or another, the implementor has to iterate through the target nodes anyway, using the provided XPath expression.
              • 4. Re: Insertion of nodes using XQuery
                NicePseronMahou
                Thank you very much for your help.