I'm writing xquery on eXist.
Usually I use this way to select item in xml:
fn:doc($document_name)/root/a
But now I wants to get the xpath from a string variable:
let $xpath := request:get-parameter("xpath", "")
fn:doc($document_name)/$xpath
Of course it doesn't work.
The only way I found now is using eval:
util:eval(fn:concat("fn:doc($document_name)", $xpath)):)
but i don't want to use eval because it's slow and not safe.
I know there's something like:
fn:doc($document_name)/*[name()='node_name']
but I want to select item via the whole path but not only the name of node
and I also have tried to use node-xpath() but don't know how to use it just like name()
You want to do what the eval() function does, so any solution is going to have the same problems as eval. The other approach you could consider is generating a query and then executing it, but it will have exactly the same problems. If you think it might be safer to restrict the string to a subset of XPath expressions (e.g. with no predicates, or no function calls) then you could try testing for those conditions using simple regular expressions.
despite Michael Kay being right, maybe the functx:dynamic-path() is of some help.
It might be a good intermediate solution sitting between fn:eval and generating the query dynamically.
Hope this helps
Michael
Related
When I evaluate this XPath expression: //superhero[n0:name="Superman"]/n1:name on this xml:
<n0:rootElement xmlns:n0='http://example.com' xmlns:n1='http://example.com'>
<superheroes>
<superhero>
<n0:name>Superman</n0:name>
<n1:name>Clark</n1:name>
</superhero>
<superhero>
<n0:name>Spiderman</n0:name>
<n1:name>Peter</n1:name>
</superhero>
</superheroes>
</n0:rootElement>
using an XPath evaluator, I get the expected result.
But when I send it to an XQuery processor, I get an error message saying that
Namespace prefix 'n0' has not been declared. Weird, huh?
It's always the prefix in the brackets (is it called a filter, maybe?) that gets the complaint.
I've used http://www.xpathtester.com to verify the difference between XPath and XQuery interpretations.
It works fine with https://codebeautify.org/Xpath-Tester which is XPath only.
If I replace n0: or n1: with *: it works in for XQuery processors, but not for XPath testers.
This is of course a toy example I've written up to clarify my issue. In production I'm calling an external service which I believe is driven by Saxon-HE. I know it accepts XQuery so I'm guessing it is in "XQuery-mode" for XPath expressions.
There isn't much I can do to the xml file since I receive it from another source. Is there a better XQuery expression I can use?
Is this a bug, or by design?
Different XPath engines provide different ways of binding the namespace prefixes used in the expression. Some, I believe, pick up the namespace bindings from the source document. So it's not a non-conformance with the standard, it's the fact that the standard leaves it up to the particular processor how the original context is established.
The underlying problem is that you probably want your query to work regardless what namespace prefixes are used in the source document. Picking up the namespace bindings from the source document is handy for ad-hoc queries, but it means that a query that does the right thing with one document will fail with a different one.
In XQuery you can declare any namespaces you want to use in your query:
declare namespace n0 = 'http://example.com';
declare namespace n1 = 'http://example.com';
//superhero[n0:name="Superman"]/n1:name
https://xqueryfiddle.liberty-development.net/bdxZ8S
See the spec at https://www.w3.org/TR/xquery-31/#id-namespace-declaration
I'm new to FreeMarker and have string I want to perform two built-ins on. For example, I have a string that needs all instances of the pipe symbol replacing with a comma and capitalizing the first letter.
I thought the syntax would be ${string?cap_first?replace("|",", ")}
But only the first built in works. I've had a look around and can't find any examples other than perhaps assigning the value to a variable and then performing the two built-ins separately.
Anybody have any ideas?
Thanks
You are using the correct syntax. Furthermore your example works for me as is. What error message you get?
There is an easy but not very elegant solution. You can chain Built-ins by wrapping them into parentheses. For instance this will work as you'd expect:
${(string?cap_first)?replace(oldString, newString)}
Hope this helps!
What I like to do is remove all functions which has specific word for example, if the word is 'apple':
void eatapple()
{
// blah
// blah
}
I'd like to delete all code from 'void' to '}'.
What I tried is:
^void.*apple(.|\n)*}
But it took very long time I think something is wrong here.
I'm using Visual Studio. Thank you.
To clarify jeong's eventual solution, which I think is pretty clever: it works, but it depends on the code being formatted in a very particular way. But that's OK, because most IDE's can enforce a particular formatting standard anyway. If I may give a related example - the problem that brought me here - I was looking to find expressions of the form (in Java)
if (DEBUG) {
// possibly arbitrary statements or blocks
}
which, yes, isn't technically regular, but I ran the Eclispe code formatter on the files to make sure they all necessarily looked like this (our company's usual preferred code style):
if (DEBUG) {
statement();
while (whatever) {
blahblahblah(etc);
}
// ...
}
and then looking for this (this is Java regex syntax, of course)
^(\s*)if \(DEBUG.*(?:\n\1 .*)*\n\1\}
did the trick.
Finally did it.
^void.*(a|A)pple\(\)\n\{\n((\t.*\n)|(^$\n))*^\}
Function blocks aren't regular, so using a regular expression in this situation is a bad idea.
If you really have a huge number of functions that you need to delete (more than you can delete by hand (suggesting there's something wrong with your codebase — but I digress)) then you should write a quick brace-counting parser instead of trying to use regular expressions in this situation.
It should be pretty easy, especially if you can assume the braces are already balanced. Read in tokens, find one that matches "apple", then keep going until you reach the brace that matches with the one immediately after the "apple" token. Delete everything between.
In theory, regular language is not able to express a sentence described by context free grammar. If it is a one time job, why don't you just do it manually.
Switch to VI. Then you can select the opening brace and press d% to delete the section.
I'm developping a mini search engine, and I want to implement the feature of searches based on logic operators AND OR...
I'm having a difficulty on parsing a query containing AND, OR, NOT... especially when it comes to parentheses... (cat or dog) not (bike not mike)
For simple AND, and OR queries, it's obviously too simple and I figured out how to formulate the sql query, but when it becomes that complicated I'm lost !!!
I'm not sure if search engines have this feature, but I want to dive into it for learning purpose.
I apologize for my last question which wasn't really clear, I hope this time I'm doing better.
I'd recommend looking at a lexer/parser generator like ANTLR. A simple grammar should be able to sort you out. There might even be an existing grammar for such a thing.
Take a look at the searchparser.py example from the pyparsing project.
It shows a way to implement:
AND,
OR,
NOT,
grouping and
wildcards.
All done in 293 lines of code (including comments and tests) ...
If you are using MySQL you can use the builtin boolean search:
http://dev.mysql.com/doc/refman/5.1/en/fulltext-boolean.html
It seems there are two ways to build queries -- either using query expressions:
IEnumerable<Customer> result =
from customer in customers
where customer.FirstName == "Donna"
select customer;
or using extension methods:
IEnumerable<Customer> result =
customers.Where(customer => customer.FirstName == "Donna");
Which do you use and why? Which do you think will be more popular in the long-run?
Only a limited number of operations are available in the expression syntax, for example, Take() or First() are only available using extension methods.
I personally prefer expression if all the required operations are available, if not then i fall back to extension methods as I find them easier to read than lambdas.
take a look at this answer,
Linq Extension methods vs Linq syntax
I use the method syntax (almost) exclusively, because the query syntax has more limitations. For maintainability reasons, I find it preferable to use the method syntax right away, rather than maybe converting it later, or using a mix of both syntaxes.
It might be a little harder to read at first, but once you get used to it, it works fairly natural.
I only use the method syntax. This is because I find it a lot faster to write, and I write a ton of linq. I also like it because it is more terse. If working on a team, its probably best to come to a concensus as to which is the preferred style, as mixing the two styles is hard to read.
Microsoft recommends the query syntax. "In general, we recommend query syntax because it is usually simpler and more readable". http://msdn.microsoft.com/en-us/library/bb397947.aspx
It depends on which you and your team find more readable, and I would choose this on a case by case basis. There are some queries that read better in syntax form and there are some that read better in method form. And of course, there is that broad middle ground where you can't say one way or the other, or some prefer it this way and others that way.
Keep in mind that you can mix both forms together where it might make it more readable.
I see no reason to suspect that either form will dissappear in the future.