Tutan Ramen
Tutan Ramen

Reputation: 1252

MarkLogic 8 - Dynamic XPath

I have a reporting requirement where a query will be exactly the same except for a few XPath expressions depending on what column needs to be filtered on in a where clause.

where $record/firstName eq $firstName

vs

where $record/lastName eq $lastName

How can I use a dynamic XPath so I can use the same code for both and pass in the path as a parameter?

Upvotes: 0

Views: 230

Answers (2)

Dave Cassel
Dave Cassel

Reputation: 8422

It sounds like you could use xdmp:unpath() for this case.

Upvotes: 2

ehennum
ehennum

Reputation: 7335

Queries and XPaths are fundamentally different:

  • Queries look for words or values in named indexes to retrieve documents.
  • XPaths find nodes within retrieved JSON or XML documents.

A subset of XPaths (known as searchable expressions) can be treated as queries behind the scenes, but there's no particular advantage to the XPaths in those cases -- the same thing can be expressed as a query.

With that as context, one possibility is to construct the queries dynamically, nesting element or JSON scope (aka container) queries to express the path. Would that meet your requirements? If not, can you expand on the use case?

One thing to be aware of: for precise scope queries, you either have to turn on positions or execute a filtered query.

Hoping that helps,

Upvotes: 1

Related Questions