Question: The First XML File **1.xml**

```
<root>
    <name>aa</name>
    <phno>bb</phno>
    <email>cc</email>
</root>

```

The Second XML File **2.xml**

```
<root>
    <name>aa</name>
    <phno>dd</phno>
    <email>ee</email>
</root>

```

When I search for "aa", using `search:search("aa")`, gives me **1.xml** and **2.xml** in search results as uri attribute.

Now, I need to convert these two documents into single JSON file with document URI in it. My approach is :  

1. Read XML document with fn:doc function.
2. add uri element to the result documents.
3. convert into Json.

How can I add `@uri` from search snippets to XML file in XQuery?  

the output should be(shouldn't update in the database):

```
<root>
    <name>aa</name>
    <phno>bb</phno>
    <email>cc</email>
    <uri>1.xml</uri>
</root>

```

I tried with my basic knowledge on XQuery like:

```
let $searchResult := search:search("aa")
let $uri := $searchResult/search:result/@uri
let $docs := 
   for $each in $uri
   return (fn:doc($each)/root, <uri>{$each}</uri>)   
return $docs

```

This gives me two separate documents, which is not I wished for..

How can I solve this?

Thanks in advance

Answer: 
You can try to reconstruct `<root>` element with additional child `<uri>` like so :

```
....
let $docs := 
   for $each in $uri
       return <root>{fn:doc($each)/root/*, <uri>{$each}</uri>}</root>
return $docs

```
Your solution is returning more than one document because you are returning a sequence of more than one document (actually, your sequence is of length "4" right now due to 2 documents and 2 unattached URI elements).

You could do something simple like this:

```
let $searchResult := search:search("aa")
let $uris := $searchResult/search:result/@uri
let $docs := element documents {
        attribute count {fn:count($uris)},
        for $uri in $uris
            return <document uri='{$uri}'>{fn:doc($uri)/node()}</uri>)   
    }
return $docs

```

This will return

```
<documents count="2">
    <document uri="1.xml">
        <root>
            <name>aa</name>
            <phno>bb</phno>
            <email>cc</email>
        </root>
    </document>
    <document uri="2.xml">
        <root>
            <name>aa</name>
            <phno>dd</phno>
            <email>ee</email>
        </root>
    </document>
</documents>

```