Now-a-days, XQuery and XPath are used heavily in all applications as XML has become a standard for data exchange in most of the applications. The modularization phase in any application development creates a need to manage the location of data dynamically.
In XPath/XQuery we all know that it doesn’t support dynamic evaluation of XPath from STRING without using EVAL function from 3rd party which is quite performance inefficient for large data. But the need for having a configuration for XPath from a specific element/node is still there.
This is again a solution to address the modularization of XQuery implementation and to address the CONSTANTs mapping for XPath which can be used by a specific XQuery dynamically.
Now, to set the context, we use XQuery LIB/SUB module to manage our constants or global data. I am taking advantage of this functionality to get a bit closer to the target goal. Below is what I configured.
1. Create a Sub/Lib module to manage the XPath which are required to be fetched dynamically from one source or different sources.
2. Create 1 liner functions for each XPath that needs to be generated dynamically.
3. Pass the source to each function in the Sub/Lib module as a start reference.
4. HardCode the exact XPath post the source in that XQuery function.
5. The XQuery functions becomes your constant XPath variable to be used.
6. Use the XQuery function as a constant in your Main XQuery.
Advantages of this approach are that it decouples the constant mapping from main XQuery file to a dedicate XPath configuration file. The only difference is that we use functions instead of variables.
Below is an example.
1. This is a sample XML for the example.
2. This is a sample LIB Module for the XQuery – Dynamic XPath function.
3. This is the main XQuery module calling 4 ways to generate the result.
Please provide your options or views on this.
In the end, it’s not the years in your life that count. It’s the life in your years.
– Abraham Lincoln