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

  • SEARCH
  • Home
  • 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 5940817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:01:19+00:00 2026-05-22T16:01:19+00:00

I am using BaseX XML Database. Consider an xml document in the database like

  • 0

I am using BaseX XML Database. Consider an xml document in the database like so:

<entries>
<book-entry>
  <book>Book 1</book>
  <author>Author 1 ABC</author>
  <title>Title 1</title>
</book-entry>
<car-entry>
  <car>Car 1</car>
  <model>Model 1</model>
  <price>Price 1 ABC</price>
</car-entry>
</entries>

I am trying to perform a search with different options such as : search across books only, cars only, both books and cars.

I am trying to use an xml variable in my xquery to return search results based on the required search type.

Example variable values:
– <types><type>book-entry</type></types> : search across book-entries only
– <types><type>car-entry</type></types> : search across car-entries only
– <types><type>book-entry</type><type>car-entry</type></types> : search across book-entries and car-entries

XQuery Sample:

declare variable $doc_name as xs:string external; (: name of xml document :)
declare variable $search_types as xs:anyAtomicType external; (: one of the example variable values shown above :)
declare variable $search_key as xs:string external; (: eg: ABC :)

for $entry in doc($doc_name)/entries[*[exists($search_types/types/type/text() = node-name(.)) and .//text() contains text $search_key]]
  return $entry

The above query returns both car and book entries which contain a text child node ABC although I pass <types><type>car-entry</type></types> to $search_types.

How do I restrict the search using an xml variable ? Is there a better way of doing this? Also, the xquery must return both cars and entries if the xml variable has child nodes of both the types.

Thanks,
Sony

  • 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-05-22T16:01:19+00:00Added an answer on May 22, 2026 at 4:01 pm
    for $entry in doc($doc_name)/entries
             [*[exists($search_types/types/type/text() = node-name(.)) 
            and 
              .//text() contains text $search_key
               ]
             ]  return $entry
    

    Must be:

    for $entry in doc($doc_name)/entries/*
            [exists($search_types/types/type/text() = node-name(.)) 
           and 
             .//text() contains text $search_key]  
      return $entry
    

    Or, alternatively, this simple XPath expression may be used:

    /*/*[name() eq $vSearchTypes/types/type
       and
         .//text()[contains(., $vSearchKey)]
        ]
    

    Finally, this XQuery expression:

    let $vSearchTypes :=
      <types>
        <type>book-entry</type>
    </types>,
    
    $vSearchKey := 'ABC'
    
    return
      /*/*[name(.) eq $vSearchTypes/type
            and
              .//text()[contains(., $vSearchKey)]
             ]
    

    when applied on the provided XML document:

    <entries>
      <book-entry>
        <book>Book 1</book>
        <author>Author 1 ABC</author>
        <title>Title 1</title>
      </book-entry>
      <car-entry>
        <car>Car 1</car>
        <model>Model 1</model>
        <price>Price 1 ABC</price>
      </car-entry>
    </entries>
    

    produces the wanted, correct result:

    <book-entry>
        <book>Book 1</book>
        <author>Author 1 ABC</author>
        <title>Title 1</title>
      </book-entry>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using xquery over BaseX XML Database. Say, I have the following documents
I built a template-based document generator using the Open XML SDK (1.0) , the
We're parsing an XML document using JAXB and get this error: [org.xml.sax.SAXParseException: Invalid byte
I want to save data from my database into xml file. I am using
I have an XML based file format that I'm using to store and load
Apple strongly recommends using the binary plist format when reading large XML-based data sets
Using progress_horizontal.xml as a base drawable for my seekbar i was able to customize
I'm using the StAX event based API's to modify an XML stream. The stream
I'm contemplating the idea of implementing a XML translator using a compiler generator, based
I have been using hbm2ddl and hbm2java to build my database and POJOs for

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.