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 7951099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:27:35+00:00 2026-06-04T02:27:35+00:00

i am new to xquery. I have the following xml document : <?xml version=1.0

  • 0

i am new to xquery. I have the following xml document :

<?xml version="1.0" encoding="UTF-8"?>
     <lines>
        <line>
            <id>1</id>
            <par>1</par>
        </line>
        <line>
            <id>2</id>
            <par>1</par>
        </line>
        <line>
            <id>3</id>
            <par>2</par>
        </line>
        <line>
            <id>4</id>
            <par>2</par>
        </line>
        <line>
            <id>5</id>
            <par>1</par>
        </line>
        <line>
            <id>6</id>
            <par>5</par>
        </line>
        <line>
            <id>7</id>
            <par>5</par>
        </line>
        <line>
            <id>8</id>
            <par>5</par>
        </line>
    </lines>

I would like to create a function that would get as input a certain id , and would return all the line elements – descendants of this id , including the one given as input. For example giving as input 1 it would return the line elements with ids 1,2,3,4,5,6,7,8. I know i can go to depth 1 using the following: lines/line[par=id_given], but what if i would like to fetch all descendants?

  • 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-04T02:27:36+00:00Added an answer on June 4, 2026 at 2:27 am

    You have to define a function which traverses the tree recursively. This one does works level-wise:

    declare function local:traverse($tree as element(lines), $id as xs:integer*) as element(line)* {
     let $level := $tree//line[par=$id][id!=$id]
     return
      if ($level)
      then ($level, local:traverse($tree, $level/id))
      else ()
    };
    

    You can call it using local:traverse(/lines, 0).

    But this function will not deal with your example XML, as this is no tree: there is a cycle in the root element which isn’t allowed. You will have to introduce some new unique root id or remove the parent from the root for having a real tree.

    <lines>
        <line>
            <id>1</id>
            <par>0</par>
        </line>
        [snip]
    

    You could also change the code above to be able to deal with these self-references: Add the predicate [id!=$id] to the end of the definition of $level, this will exclude self-references. Now your root node is excluded from the results, reinclude it in the function call: (/lines/line[id=1], local:traverse(/lines, 1)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have several Section elements in an XML document, what XQuery do I
What would the XQuery look like to check if a node exists, and if
Is it possible to put each XQuery result on a new line? I thought
I am new to xquery in SQL Server. I have often come across xquery
I am quite new to Marklogic and xquery in general. We have a collection
Say I have a Java String which has xml data like so: String content
I have an xml file <?xml version=1.0?> <category> <name>SWEATERS</name> <name>WATCHES</name> <name>PANTS</name> <name>test</name> <name>1</name> </category>
I'm pretty new to XQuery and I'm trying to write an example function that
New to Rails, and following a tutorial that starts by creating the classic blog
New to Mongo, taking on the analytics quest and have a schema question. I'm

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.