Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming the input XML is in file <code>in.xml</code> this XQuery script does what you want:</p> <pre><code>(: This library function can be found here: http://www.xqueryfunctions.com/xq/functx_index-of-node.html :) declare namespace functx = "http://www.functx.com"; declare function functx:index-of-node($nodes as node()* , $nodeToFind as node() ) as xs:integer* { for $seq in (1 to count($nodes)) return $seq[$nodes[$seq] is $nodeToFind] }; (: Recursively calculate the start elements with the other elements between as childs. Take the first two indices of $positions and create a start element with the elements of $elements with positions between these two indices. Then remove the first index of $position and do the recursive call. Input: $positions: Sequence with start element indices (belongs to $elements) $elements: Element sequence Output: Sequence of start elements with child elements :) declare function local:partition($positions as xs:integer*, $elements as element()*) as element()* { let $len := count($positions) return if($len gt 1) then ( let $first := $positions[1] let $second := $positions[2] let $rest := subsequence($positions, 2) return ( element start { subsequence($elements, $first + 1, $second - $first - 1) }, local:partition($rest, $elements) ) ) else if($len eq 1) then ( element start { subsequence($elements, $positions[1] + 1) } ) else () }; (: Input document :) let $input-doc := doc('in.xml') (: Sequence of all child elements of root element doc :) let $childs := $input-doc/doc/node()[. instance of element()] (: Sequence with the indices of the start elements in $childs :) let $positions := for $s in $input-doc/doc/start return functx:index-of-node($childs, $s) return &lt;doc&gt; { local:partition($positions, $childs) } &lt;/doc&gt; </code></pre> <p>The output is:</p> <pre><code>&lt;doc&gt; &lt;start&gt; &lt;a/&gt; &lt;b/&gt; &lt;item/&gt; &lt;item/&gt; &lt;item/&gt; &lt;/start&gt; &lt;start&gt; &lt;item/&gt; &lt;item/&gt; &lt;item/&gt; &lt;/start&gt; &lt;start&gt; &lt;b/&gt; &lt;item/&gt; &lt;item/&gt; &lt;item/&gt; &lt;/start&gt; &lt;/doc&gt; </code></pre> <p>Testet with <a href="http://xqilla.sourceforge.net/HomePage" rel="nofollow">XQilla</a> but every other XQuery processor should produce the same result.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload