I want to fetch a data from xml Using Xquery with starts with function.
data.xml
<data><employee id=\"1\"><name value=\"vA-12\">A</name> <title id=\"2\">Manager</title></employee>
<employee id=\"2\"><name value=\"vC-12\">C</name><title id=\"2\">Manager</title></employee>
<employee id=\"2\"><name value=\"vB-12\">B</name><title id=\"2\">Manager</title></employee>
</data>
Now I want to fetch that name which has employee#id=title#id and name#value starts with 'vC'.
I have written this xquery for the same.Please see below but getting error-
for $x in /data/employee where $x/#id=$x/title/#id and [fn:starts-with($x/name/#value,vC)] return data($x/name)
this is error-
Error on line 1 column 55
XPST0003 XQuery syntax error near #.../title/#id and [fn:starts-with#:
Unexpected token "[" in path expression
net.sf.saxon.s9api.SaxonApiException: Unexpected token "[" in path expression
at net.sf.saxon.s9api.XQueryCompiler.compile(XQueryCompiler.java:544)
at Xml.process(Xml.java:46)
at Xml.main(Xml.java:30)
Caused by: net.sf.saxon.trans.XPathException: Unexpected token "[" in path expression
at net.sf.saxon.query.XQueryParser.grumble(XQueryParser.java:479)
at net.sf.saxon.expr.parser.XPathParser.grumble(XPathParser.java:221)
starts-with() function expects two parameters. I'm not familiar with Saxon specifically, but in general xquery you can do this way :
for $x in /data/employee[#id=title/#id and name[starts-with(#value,'vC')]]
return data($x/name)
or using where clause instead of predicate in for clause, as in your attempted query :
for $x in /data/employee
where $x/#id=$x/title/#id and $x/name/starts-with(#value,'vC')
return data($x/name)
Just use this shorter and simpler XPath 2.0 one-liner:
/*/employee[#id eq title/#id and starts-with(name/#value, 'vC')]/data(name)
Depending on your exact requirements, you might need this instead (I let you go through the differences, but basically it selects all names with #value starting with vC, where the other solutions here select all names of all employees with at least a name starting with vC):
/data/employee[#id eq title/#id]/name[starts-with(#value, 'vC')]/data(.)
Related
hello i'm stuck in this problem i have a query that take in setting the value of $ {body}
<to uri="sql:SELECT distinct substr(cust_account,4,3) as TypeCompte from bnaservice.customer_accounts where cust_account like '#${body[0]}%' order by cust_account?dataSource=moodleDB"/>
<to uri="bean:tn.ngtrend.CompteClientRest.Transformer?method=ToXml(Exchange)"/>
the value of $ {body} equal 001 but each time I execute the query it gives me a result null
I think the concatenation between $ {body} and% is wrong
is there anyone who can help me solve this problem,thanks
Can you try to use a colon? Because body is like a named parameter.
like ':#${body[0]}%'
If this still does not work, I would try to concatenate the expression and the % in an Exchange property and then call the property in the SQL statement
Finally I get this working with this snippet:
(It's scala but it's practically the same)
transform("%" + _.in[String] + "%")
to("sql:SELECT * FROM quotes WHERE quote LIKE :#${body} ORDER BY RANDOM() LIMIT 1")
While doing an Import of an XML, am getting the following validation error:
"Error at line 5 column 76: The value 'nodecapacity' of attribute 'name' on element 'parameter' is not valid with respect to its type, 'name'"
I am using a SAX Parser.
Also, is there a way I can get the expected regex pattern to be displayed in the error message?
Try a different schema processor such as Saxon to see if the error messages suit you better. For example Saxon produces this:
Validation error on line 1 column 15 of test.xml:
XSD99999: The content "xxx" of element <top> does not match the required simple type.
Value "xxx" contravenes the pattern facet "[0-9]*" of the type nameType
I figured out that of course . and SPACE aren't allowed. Are there other forbidden characters ?
You can use any (UTF8) character in the field name which aren't
special (contains ".", or starts with "$").
https://jira.mongodb.org/browse/SERVER-3229
https://stackoverflow.com/a/7976235/311220
It's generally best to stick with lowercase alphanumeric with underscores though.
Something else to look out for is the fact that you can make a property name called "query" but then use query operators on it, making it awkward to do a large number of queries.
Example:
Insert document with a property named
db.coll.insert({ query: 'foo' });
Equality query works:
db.coll.findOne({ query: 'foo' });
Not equal ($ne) does not:
db.coll.findOne({ query: { $ne: 'bar' } });
How to select an XML using XPath without considering namespace and prefix
tried
/*:OrderPerson/OrderUser/
But returns error
org.jdom.JDOMException: Invalid XPath expression: ....
Unexpected ':'
You can try this expression
/*[local-name()='OrderPerson']/OrderUser/
Your xpath query has not a valid syntax
try with
/OrderPerson/OrderUser/*
I have the following xml file:
<author>
<firstname>Akhilesh</firstname>
<lastname>Singh</lastname>
</author>
<author>
<firstname>Prassana</firstname>
<lastname>Nagaraj</lastname>
</author>
And I am using the following JXPath expression,
concat(author/firstName," ",author/lastName)
To get the value Akhilesh Singh ,Prassana Nagaraj but
I am getting only Akhilesh Singh.
My requirement is that I should get the value of both author by executing only one JXPath expression.
XPath 2.0 solution:
/*/author/concat(firstname, ' ', lastname, following-sibling::author/string(', '))
With XPath 1.0, when an argument type other than node set is expected, the first node in the node set is selected and then apply the type conversion (boolean type conversion is some how different).
So, your expresion (Note: no capital):
concat(author/firstname," ",author/lastname)
It's the same as:
concat( string( (author/firstname)[1] ), " ", string( (author/lastname)[1] ) )
Depending on the host language you could use:
author/firstname|author/lastname
This is evaluate to a node set with firstName and lastName in document order, so then you could iterate over this node set extracting the string value.
In XPath 2.0 you could use:
string-join(author/concat(firstname,' ', lastname),' ,')
Output:
Akhilesh Singh ,Prassana Nagaraj
Note: Now, with sequence data type and function calls as steps, XPath resembles the functional language it claims to be. Higher Order Functions and partial applycation must wait to XPath 2.1 ...
Edit: Thanks to Dimitre's comments, I've corrected the string separator.
concat() will return single string. If you want both results then you need to iterate over "author" element and do "concat(firstName," ",lastName)"