Marklogic: How to extract some element only from search result?

108 views Asked by At

I have one xml document

sample.xml

<cities>
 <id>1<id>
 <country>India</country>
 <city>Mumbai</city>
</cities>
<cities>
 <id>2<id>
 <country>US</country>
 <city>California</city>
</cities>
<cities>
 <id>3<id>
 <country>India</country>
 <city>New Delhi</city>
</cities>

I want to perform search on Id and On the basis of id I want to fetch only one element. For ex: if I search for id=1 I want

<cities>
 <id>1<id>
 <country>India</country>
 <city>Mumbai</city>
</cities>

only this to be returned

This would have been easy if cities were part of different xml's, I would have simply done

element-value-query

any suggestions how to tackle this with all the cities being part of one xml only?

2

There are 2 answers

1
Sunny On

I got that working using FLOWR

for $x doc("path/sample.xml")/cities
where $x/id='1'
return $x
0
Vennela On

Use the below if is not the root element:

cts:search(//cities, cts:element-value-query(xs:QName("id"), "1"))

Use the below if is the root element:

cts:search(/cities, cts:element-value-query(xs:QName("id"), "1"))