Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

how to move nodes

2009-06-10
2013-06-05
  • Erdal Karaca
    Erdal Karaca
    2009-06-10

    Input:

    <root>
      <r id="r1"><contents>r1</contents></r>
      <r id="r2"><contents>r2.1</contents></r>
      <r id="r2"><contents>r2.2</contents></r>
      <r id="r2"><contents>r2.3</contents></r>
      <r id="r3"><contents>r3</contents></r>
    </root>

    Requirement:
    modify the input such that this will be the output:

    <root>
      <r id="r1"><contents>r1</contents></r>
      <r id="r2">
         <contents>r2.1</contents>
         <contents>r2.2</contents>
         <contents>r2.3</contents>
      </r>
      <r id="r3"><contents>r3</contents></r>
    </root>

    I need to identify all nodes named "r" whose attributes are equal, then move the contents of the identified nodes into a single node...

    XQJ suggests the "Move Node" ability. Is this supported by mxquery. How to solve/simulate this if it is not supported?

    Thanks!

     
    • Hi,

      where did you find the "move node" ability in XQJ - I'm not familiar with it, and a Google search did not deliver any results

      To solve your problem, there are two separate concerns that need to be adressed:
      1) How to put the nodes into the right "group"
      2) Updating the original document or producing a new output

      I'll discuss 2) first:
      Do "input" and "output" need to be in the same file, DOM tree or do you just need to produce "output" from "input"?
      If the latter is the case, you don't need the update facility at all (or maybe just for modify/copy), "normal" XQuery is sufficient

      Regarding 1), there are three solutions:
      If you just want to use XQuery 1.0, you need to express this grouping by a nested loop
      for $a in distinct-values($input/root/r/@id)
        for $b := $input/root/r/@id
      return ...

      (see also http://www.w3.org/TR/xquery/#id-grouping\)

      In XQuery 1.1, there are two more elegant solutions:
      If the id values are sorted, the "for tumbling window" expression can create ranges with the same id value
      If the id values are not sorted, the group by expression can create groups with the same id value

      see (http://www.w3.org/TR/xquery-11-use-cases/#windowing and http://www.w3.org/TR/xquery-11-use-cases/#groupby\)

      MXQuery 0.6.0 implements windowing, groupby will be part of the next release

      Regards,
      Peter

       
    • Erdal Karaca
      Erdal Karaca
      2009-06-10

      Sorry, I just mixed up xquery/xqj...

      http://www.w3.org/TR/xquery-update-10-requirements/
      3.4.7 Moving nodes

      Though, this is marked as "[...]has not been met[...]".
      I am not familiar with w3c processes and thus do not know what that status is all about...

      Thanks for your suggestions, I will try them out.

       
    • The import part there is the issue of "node identity". If you don't care about it (e.g. you are just interested in the new XML "text"), moving nodes can be performed by insert/delete.
      If you do care, there is a problem :)