XQuery Invalid entity reference error caused by "&" entity reference - xquery

I'm trying to run this line xdmp:unquote(concat('<info>', string( $paragraph) , '</info>')) but I've got the following error: xdmp:unquote("<info>LEARNING & MEMORY</info>") -- Invalid entity reference " " at line 1. It seems like this entity reference & is causing the problem. I tried to remove it using replace function but it still present. What should I do?

I am assuming that you have something like this-
let $paragraph := <p>LEARNING & MEMORY</p>
return
xdmp:unquote(fn:concat('<info>', fn:string($paragraph),'</info>'))
And that the result you want is XML that looks like-
<info>LEARNING & MEMORY</info>
The ampersand is definitely the issue and the workaround is to use the "repair-full" option. This example works:
let $paragraph := <p>LEARNING & MEMORY</p>
let $contents := xdmp:unquote($paragraph, "", "repair-full")
return
<info>{$contents}</info>

Related

SQL query error or missing database shows syntax error using LIKE query

I have a syntax error that I don't know how to solve. Here is the code:
fun allSearchData(searchStringRaw: String): Cursor{
val searchString = "% $searchStringRaw %"
val query = "Select * from $MARKER_TABLE where $KEY_NAME like $searchString"
val c = mySpotsDatabase!!.rawQuery(query, null)
c.moveToFirst()
return c
}
and it is giving me this error:
Error Code : 1 (SQLITE_ERROR)
Caused By : SQL(query) error or missing database.
(near "%": syntax error (code 1): , while compiling: Select * from my_locations_table where Name like % h %)
If I remove the spaces as follows it makes no difference.
val searchString = "%$searchStringRaw%"
and this just changes the error line to the following, so I know its not the space in the string issue.
(near "%": syntax error (code 1): , while compiling: Select * from my_locations_table where Name like %h%)
I can not see the syntax error. Any help would be appreciated.
You need to enclose the argument for the LIKE clause in single quotes.
e.g. val searchString = "'% $searchStringRaw %'"
However, you would be better of using the second parameter of the rawQuery method to pass the arguments, in which case they will be properly escaped and you gain the advantage of protecting against SQL Injection.
Thanks To #MikeT who saw the issue.
Here is the correct code:
fun allSearchData(searchStringRaw: String): Cursor{
val searchString = "'%$searchStringRaw%'"
val query = "Select * from $MARKER_TABLE where $KEY_NAME like $searchString"
val c = mySpotsDatabase!!.rawQuery(query, null)
c.moveToFirst()
return c
}
It now works perfectly

How to match space in MarkLogic using CTS functions?

I need to search those elements who have space " " in their attributes.
For example:
<unit href="http:xxxx/unit/2 ">
Suppose above code have space in the last for href attribute.
I have done this using FLOWER query. But I need this to be done using CTS functions. Please suggest.
For FLOWER query I have tried this:
let $x := (
for $d in doc()
order by $d//id
return
for $attribute in data($d//#href)
return
if (fn:contains($attribute," ")) then
<td>{(concat( "id = " , $d//id) ,", data =", $attribute)}</td>
else ()
)
return <tr>{$x}</tr>
This is working fine.
For CTS I have tried
let $query :=
cts:element-attribute-value-query(xs:QName("methodology"),
xs:QName("href"),
xs:string(" "),
"wildcarded")
let $search := cts:search(doc(), $query)
return fn:count($search)
Your query is looking for " " to be the entirety of the value of the attribute. If you want to look for attributes that contain a space, then you need to use wildcards. However, since there is no indexing of whitespace except for exact value queries (which are by definition not wildcarded), you are not going to get a lot of index support for that query, so you'll need to run this as a filtered search (which you have in your code above) with a lot of false positives.
You may be better off creating a string range index on the attribute and doing value-match on that.

xQuery substring problem

I now have a full path for a file as a string like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
However, now I need to take out only the folder path, so it will be the above string without the last back slash content like:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/"
But it seems that the substring() function in xQuery only has substring(string,start,len) or substring(string,start), I am trying to figure out a way to specify the last occurence of the backslash, but no luck.
Could experts help? Thanks!
Try out the tokenize() function (for splitting a string into its component parts) and then re-assembling it, using everything but the last part.
let $full-path := "/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
$segments := tokenize($full-path,"/")[position() ne last()]
return
concat(string-join($segments,'/'),'/')
For more details on these functions, check out their reference pages:
fn:tokenize()
fn:string-join()
fn:replace can do the job with a regular expression:
replace("/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"[^/]+$",
"")
This can be done even with a single XPath 2.0 (subset of XQuery) expression:
substring($fullPath,
1,
string-length($fullPath) - string-length(tokenize($fullPath, '/')[last()])
)
where $fullPath should be substituted with the actual string, such as:
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml"
The following code tokenizes, removes the last token, replaces it with an empty string, and joins back.
string-join(
(
tokenize(
"/db/Liebherr/Content_Repository/Techpubs/Topics/HyraulicPowerDistribution/Released/TRN_282C_HYD_MOD_1_Drive_Shaft_Rev000.xml",
"/"
)[position() ne last()],
""
),
"/"
)
It seems to return the desired result on try.zorba-xquery.com. Does this help?

How to use ? : if statements with Razor and inline code blocks

I'm updating my old .aspx views with the new Razore view engine. I have a bunch of places where I have code like this:
<span class="vote-up<%= puzzle.UserVote == VoteType.Up ? "-selected" : "" %>">Vote Up</span>
Ideally I'd like to do this:
<span class="vote-up#{puzzle.UserVote == VoteType.Up ? "-selected" : ""}">Vote Up</span>
However there's two problems here:
vote-up#{puzzle.UserVote .... is not treating the # symbol as a start of a code block
#puzzle.UserVote == VoteType.Up looks at the first part #puzzle.UserVote as if it's supposed to render the value of the variable.
Anyone know how to address these issues?
This should work:
<span class="vote-up#(puzzle.UserVote == VoteType.Up ? "-selected" : "")">Vote Up</span>
#( condition ? "true" : "false" )
The key is to encapsulate the expression in parentheses after the # delimiter. You can make any compound expression work this way.
In most cases the solution of CD.. will work perfectly fine. However I had a bit more twisted situation:
#(String.IsNullOrEmpty(Model.MaidenName) ? " " : Model.MaidenName)
This would print me " " in my page, respectively generate the source &nbsp;. Now there is a function Html.Raw(" ") which is supposed to let you write source code, except in this constellation it throws a compiler error:
Compiler Error Message: CS0173: Type of conditional expression cannot
be determined because there is no implicit conversion between
'System.Web.IHtmlString' and 'string'
So I ended up writing a statement like the following, which is less nice but works even in my case:
#if (String.IsNullOrEmpty(Model.MaidenName)) { #Html.Raw(" ") } else { #Model.MaidenName }
Note: interesting thing is, once you are inside the curly brace, you have to restart a Razor block.

ASP conditional error

i am trying to use an ASP conditional here:
if (Request.Cookies("username")) and
(Request.Cookies("password")) <> ""
Then
And i keep getting this error:
Type mismatch: '[string: ""]'
Any ideas what I am getting that?
try
if (Request.Cookies("username") <> "") and (Request.Cookies("password") <> "") Then
Actually, I would do the following..
if (!string.IsNullOrEmpty(Request.Cookies("username")) &&
!string.IsNullOrEmpty(Request.Cookies("password")))
{
// Do your stuff, here :)
}
Get into the habit of using string.IsNullOrEmpty for testing variables and string.Empty for setting values, if u don't want a string to be null.

Resources