Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:55:23+00:00 2026-06-07T13:55:23+00:00

I’m troubled with an exercise on recursive functions in Xquery. I need to transform

  • 0

I’m troubled with an exercise on recursive functions in Xquery. I need to transform a flat XML tree to a nested XML tree using only Xquery.

The flat XML looks like this:

    <?xml version="1.0"?>
    <tree>
      <node id="1" type="Folder">
        <child>2</child>
        <child>3</child>
      </node>
      <node id="2" type="Folder">
        <child>4</child>
      </node>
      <node id="3" type="Folder">
        <child>5</child>
        <child>6</child>
      </node>
      <node id="4" type="Item" />
      <node id="5" type="Folder">
        <child>7</child>
      </node>
      <node id="6" type="Item" />
      <node id="7" type="Item" />
    </tree>

The nested XML required looks like this:

    <?xml version="1.0"?>
    <tree>
      <node id="1" type="Folder" children="2 3">
        <node id="2" type="Folder">
          <node id="4" type="Item" />
        </node>
        <node id="3" type="Folder">
          <node id="5" type="Folder" children="7">
            <node id="7" type="Item" />
          </node>
          <node id="6" type="Item" />
        </node>
      </node>
    </tree>

I’ve tried to Xquery this without recursive functions, but without much luck. Especially the conditioning seems strange to me; the root element of the new nested XML should be the node with id=”1″, since it does not exist as a child of any other element. However, if I try to specify this as a condition, e.g. below, it does not seem possible to just select that node.

    for $node in /Tree/node[@id != /Tree/node/child]
    return
    <node id="{data($node/@id)}" type="{data($node/@type)}">

            (: Lower level subqueries.... :)

    </node>

UPDATE: I’ve come a lot further, but now I’m stuck with the [condition] on the selection of nodes that have an id equal to the contents of a parent node, i.e. the for statement in the recursive function does not give back any nodes, which is unexpected.

This is my code thus far:

    declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
    declare namespace local = "localhost";
    declare option output:method "xml";
    declare option output:omit-xml-declaration "no";
    declare option output:indent "yes";
    declare option output:doctype-system "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";

    declare function local:addSubNode($n)
    {
        for $subnode in doc("xml/flat-tree.xml")/tree/node[@id = $n]
            let $subnid:=data($subnode/@id)
            let $subtype:=data($subnode/@type)
        return <node id="{$subnid}" type="{$subtype}">
                {local:addSubNode($subnid)}
                </node>
        };

    <tree>

    {
    for $node in doc("xml/flat-tree.xml")/tree/node[not(@id = /tree/node/child)]
    let $nid:=data($node/@id)

    return <node id="{$nid}" type="{data($node/@type)}"> 
        {for $child in $node
        let $cid:=data($child)
        return local:addSubNode($cid)
        }

            </node>
    }

    </tree>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:55:25+00:00Added an answer on June 7, 2026 at 1:55 pm

    Ok got it! Eventually I fed the whole $node or $child element to the recursive function instead of just its id attribute, now it works!

        declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
        declare namespace local = "localhost";
        declare option output:method "xml";
        declare option output:omit-xml-declaration "no";
        declare option output:indent "yes";
        declare option output:doctype-system "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd";
    
        (:lookupChildren function :)
        declare function local:lookupChildren($p)
        {
        for $child in $p/child
        return $child
        };
    
        declare function local:addSubNode($n)
        {
        for $subnode in doc("xml/flat-tree.xml")/tree/node
                let $subnid:=data($subnode/@id)
                let $subtype:=data($subnode/@type)
        where $subnode/@id = $n/child
            return 
            <node id="{$subnid}" type="{$subtype}" children="{local:lookupChildren($subnode)}">
                   {local:addSubNode($subnode)}
                    </node>
        };
    
        <tree>
    
        {
        for $node in doc("xml/flat-tree.xml")/tree/node[not(@id = /tree/node/child)]
        let $nid:=data($node/@id)
        let $ntype:=data($node/@type)
    
        return  <node id="{$nid}" type="{$ntype}" children="{local:lookupChildren($node)}"> 
                {local:addSubNode($node)}
    
                </node>
        }
    
        </tree>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.