XQuery: generate a variable name from string (attribute value) - xquery

I have a framework which runs my xquery and it includes an XML config file that defines the parameters I can use in the query as variables, like this:
<text name="myVar" label="My Var"/>
This means that the user will have a dialog with a field called "My Var", whose value I can then access as $myVar in my query. I don't know how the variable is declared and have no access to the XQuery code that runs my query.
But I want to access my params in a generic way, e.g. go through all of them, check if empty, create a table with labels and values in HTML. I do have access to the XML config file. That means I have to access the value of
$(/config/text/#name)
(The above does not work obviously).
How is it possible?

Well, variable names such as $myVar can only be defined statically, so this isn't going to be possible unless you actually generate the source code of your query programmatically from what you find in the config file.
I think what I would do here is to pass the parameters as a map:
declare variable $params as map(xs:string, item()*);
do-something-with($params('myVar'));
The entries in the map can be accessed either statically ($params?myVar) or dynamically ($params('myVar'))

Related

Executing a method which is named via a config file

In short: I have a method name provided via a JSON configuration file. I'd like to call a method using this provided name. The method (with a matching name) will exist in the backend. What's the best way of going about this?
I am not quite sure what I should be searching for as an example.
To detail: I am working with a legacy application, hence the VB.NET. I am building a single PDF file from multiple PDF sources. Most of these are as is, I simply read the configuration and grab the relevant files and the job is done. However some require processing, I'd like the configuration file to pass in a method name to be called that will perform extra processing on the PDF, whatever that may be.
As there can be a lot of PDF files that can vary, I cannot simply use a property such as "PostProcessing: true".
Any ideas?
You could use reflection to reflect method names back and check them against the name passed from the property in the config file.
Like so
Type magicType = Type.GetType("MagicClass");
MethodInfo magicMethod = magicType.GetMethod("ItsMagic");
object magicValue = magicMethod.Invoke(magicClassObject, new object[]{100});
That would work.. but to be honest, I'd go with a case statement as you'll be hardcoding the method names anyway (because they are code), and it'll be strongly typed (less chance of typos and errors).

global variable in optaplanner

In drools rule file how do I set the value of (or initialize) a global variable for optaplanner. My use case is following:
I want to declare a global java map that is constant and will not change during the execution. Every rule will access the map to check for a value, if value is in the map then rule will evaluate to false. The map is being generated before execution starts by accessing data in files/database.
This link https://issues.jboss.org/browse/PLANNER-94 is also requesting the access to global variable but this feature is rejected now.
How do i use the hack defined in this link: Setting global variables in working memory in Drools planner, from which object i should get CustomSolverPhaseCommand? [I am not able to comment on this post yet because i don't have enough reputation, sorry if it seems duplicate question].
I am creating SolverFactory from xml Resource and xml file contains the path to .drl file. Just like in .drl we can access object HardSoftScoreHolder scoreHolder, I want to access the map in the same way in 'then' part of rule.
can anyone please help?
Look at the examples that have a class that ends with Parameterization instead, such as ConferenceParametrization, this is probably a better alternative than globals.

Map optional element to required element

In my source schema I have an optional element, in my destination I have a required element.
If the source exists I have to map it directly to the destination, otherwise I need to generate a GUID and assign it to the destination. How do I do this?
If you're not using xslt for your mapping:
Test for existence using the "Logical Existence" functoid -> "Logical Not" functoid -> "Value Mapping" functoid. This checks to see if the node in the input does not exist, if it does not, then use the value mapping to set the value. You can put a hard coded value in the value mapper directly, or hook it to some other input. You may need to do a bit more work if your input node exists but is blank.
There are lots of examples on the internet.

Custom configuration section and app.config

I have some incoming XML documents, I need to check attributes that are configurable in this coming XML and if the attribute is not there in the XML document being checked add it.
The attributes to be checked will be specified in the app.config file
OrderRequest.orderDetails.orderSummary.testCount, I need to check if the testCount attribute exists under the element OrderRequest. If No add it along with it’s value which is also specified in the app.config
INCOMING XML --> Read the attributes from the app.config which need to be checked in this XML file --> Read the incoming XML using XDocument --> If the attribute is not there in the incomg XML add it to the XML.
Any ideas how I can go on this.
I wrote a series of articles on how to create custom configuration sections, which can be found here: http://dotnetslackers.com/articles/CustomConfiguration/default.aspx
Is this a static development task? What I mean is, are you only testing the testCount attribute, or is this generic to say loop through the element type and check all of its attributes to ensure they are there?
Either way, essentially you have to find an element, get its name, access your custom configuration section and lookup the name, use the GetAttribute method to check for attribute existence, and append a new attribute to the element if not.
HTH.

Cannot access constant through itemRenderer using parentDocument

I have an itemRenderer inside a dataGrid, and I am able to access variables on the mxml file (in a *.as script file referenced from the mxml) using parentDocument. However, I am unable to access a constant in the same script file. If I change the constant to a regular var, I can access it.
I created a getter function for the constant and it works, but why can't the constant be
accessed directly?
Thanks
Constants are generally static, and therefore access via the class. Getters / Setters are members, and therefore accessed via an instance.
Therefore, to get access to a constant, you need to have an explicit reference to the class.
Something like parentDocument.MY_CONSTANT wouldn't work, however MyClass(parentDocument).MY_CONSTANT would.

Resources