Link to home
Start Free TrialLog in
Avatar of Himans Ghost
Himans Ghost

asked on

Encoding XPath string in SelectChildNode having both single and double quotes in C++

I have been trying to pass a string to fetch the node and am successful for string with only single quote (') and only double quotes ("). But am unable to parse it when string contains both singe and double quotes. I have my string in CString as-

CString str=L("H'el"lo");
and all other combinations of these. Can you please tell me how to do in C++, i have seen the examples are in C# but that are not helping me out.

Here's the link for C# Encoding XPath Expressions with both single and double quotes

XmlNode n = doc.SelectSingleNode(“/root/emp[lname=" + str + "]“);
How should i make my str work for string containing both single and double quotes in any order.
Avatar of sarabande
sarabande
Flag of Luxembourg image

CString str=_T("H\'el\"lo");

Open in new window


you should 'escape' both quote characters by prefixing them with a backslash \.

Sara
doc.SelectSingleNode(“/root/emp[lname=" + str + "]“);

if the str was correctly built, it  doesn't matter which special characters are included. however, i have doubts that the

         /root/emp[lname=...]

is a valid expression within an xml document if the ... contains quote characters.

Sara
Avatar of Himans Ghost
Himans Ghost

ASKER

No it isn't working that's why i posted it i tried using this already
No it isn't working that's why i posted it i tried using this already

Open in new window



can you post the error and a screenshot of the contents that is in str? actually, the statement

CString str=L("H'el"lo");

Open in new window


doesn't compile. if you get a compile error do you say 'it isn't working' or is it a different string contents to that what you posted?

i also didn't quite understand in your original post what you mean by "the link for C# Encoding XPath Expressions with both single and double quotes". actually the code contains only normal double-quoted literals and not a mix of single quotes and double quotes as stated.

Sara
this was the solution i foundin c# i wanted similar in c++ this was the link i was referring to.

public static string XpathExpression(string value)
{
    if (!value.Contains("'"))
        return '\'' + value + '\'';

    else if (!value.Contains("\""))
        return '"' + value + '"';
    else
        return "concat('" + value.Replace("'", "',\"'\",'") + "')";
}

there are no compile errors i am facing the error here -

XmlNode n = doc.SelectSingleNode(“/root/emp[lname=" + str + "]“);

str contains "H\'el\"lo" in debug mode when i checked its value  as suggested i tried already with escape sequences and without. it fails when it tries to search the node with specified string as it thinks any single quote or double quote are ending the string beyond going further. It returns com_error memory exception.
ASKER CERTIFIED SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey what will happen to the concat ? as it is not a c++ function?
return "concat('" + value.Replace("'", "',\"'\",'") + "')";

Open in new window


I already told you to try

return "concat('" + purified_value + "')"; 

Open in new window



the concat is neither a c# function nor a c++ function (in c# there is Concat function with a capital C letter at the begin. so concat is text here and the string returned is

   
"concat('ppppppppp' )"

Open in new window


where pppppppp is the 'purified value' which is the text of value where all quote letters are removed.

so the concat should be a script function if the return value really is that what was expected. I have some doubts because a concat function normally has two arguments, beside if it is a member function of a string class what is not much likely to be the case.

so i would assume that the return statement with the concat is a bug which never made problems because it is not very likely that value contained both single quote and double quote characters.

if i am right you simply should do

     return "\"" + purified_value + "\"";

Open in new window


what is to return a literal between double quotes and a value where all quote characters were removed.

Sara
Thanks for the help :) but i think i should change my approach it is not supporting both single and double quotes in string, and also since concat is not a built in function in c++ so i don't want my string with concat value.
There was no final response from the Questioner but as the problem turned out to be a misinterpretation of a sequence of quote characters,  where '"' was not recognized as single-quote double-quote single-quote, my last comment should be a valid solution. Please object to this recommendation if you think that the problem was not solved or if the explanation was not clear.

Sara