[xquery-talk] joining multiple queries into a single one
mhk at mhk.me.uk
Wed Apr 12 00:06:06 PDT 2006
Firstly, a query does not have to be a FLWOR expression! This:
for $r in //task where $r/prolog/meta/filename/text()="xys" return $r
is just a rather long-winded way of writing
Secondly, that "/text()" at the end of the path expression is really bad
practice. Unfortunately it's sometimes used by people who ought to know
better. There are several good reasons to avoid it:
(a) it causes the query to fail or produce wrong results if the source XML
contains comments - queries should normally be written so they aren't
affected by comments.
(b) if the data is typed, you are throwing away the type information and
possibly causing unnecessary data conversions.
(c) It's just plain redundant: write //task[prolog/meta/filename = "xys"]
Thirdly, the "task" seems to be the outermost element in your document. Use
/task rather than //task to avoid a search of the whole document. [This
might not apply to eXist: but it never does any harm to give the system more
information to narrow the search.]
Now to your question. If you don't care what the outermost element is
/*[prolog/meta/filename = "xys"]
If you want to match an outermost element called "task" or "concept" but
nothing else, use
/(task|concept)[prolog/meta/filename = "xys"]
> -----Original Message-----
> From: talk-bounces at xquery.com
> [mailto:talk-bounces at xquery.com] On Behalf Of Titash Neogi
> Sent: 11 April 2006 11:08
> To: talk at xquery.com
> Subject: [xquery-talk] joining multiple queries into a single one
> Dear All,
> Please consider the following scenario.
> I have XML content in a database. These files are of four categories.
> tasks, concepts, refs and topics.
> Task files are contained in <task> </task>
> Concept files with <concept></concept>
> And so on.
> Withing each file there is a metadata that can be used for querying.
> This metadata is similar for all for types of files.
> <filename>FILENAME A</filename>
> <filename>FILENAME B</filename>
> And so on for other files.
> My problem is -
> I am trying to a write a query that can search and extract
> content from
> all these types of files. I know how to write a query for individual
> file types but I want to write a single query for all four file types.
> Here is an example -
> Currently my query looks something like this
> for $r in //task where $r/prolog/meta/filename/text()="xys" return $r
> for $r in //concept where
> $r/prolog/meta/filename/text()="xys" return $r
> I want to do away with this redundancy and write a single
> query for this
> which works on all four file types in the database.
> I am using the eXist Native XML Database. My content files
> are the path
> If I write a query such as
> For $r in /db/content where $r//prolog/meta/filename/text()="xys"
> this doesn't seem to work.
> Please advice.
> Thanks and regards
> talk at xquery.com
More information about the talk