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.
Himans GhostAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window


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

Sara
1
sarabandeCommented:
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
0
Himans GhostAuthor Commented:
No it isn't working that's why i posted it i tried using this already
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

sarabandeCommented:
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
0
Himans GhostAuthor Commented:
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.
0
sarabandeCommented:
return '\'' + value + '\'';

the '\'' is 4 letters: a single quote, a backslash, a single quote, and another single quote.

the '"' is 3 letters, a single quote, a double quote, and a single quote.

it is the c, c++ and c# way to specify a single quote letter ' and a double quote letter as a char.

so the c# code does the following:

it tests whether the input value already has single quotes in the value string. if not, it adds single quote letters at begin and end of value. if the value doesn't contain double quotes it adds double quote letters at begin and end of value. for all other cases (it either has already single quote letters or double quote characters) it adds double quotes at begin and end of the purified value where all quote letters were removed before.

in c++ you would do

CString XpathExpression(const CString & value)
{
    // the _T would add L in case of UTF16 characters and UTF16 strings
    if (value.Find(_T("'")) < 0)   // that is double quote and single quote and double quote
        return _T('\'') + value + _T'\'');

    else if (value.Find(_T("\"")) < 0)
        return _T('"') + value + _T('"');   // the CString::operator+ can add characters both left and right
    else
    {
        CString purified_value = value;
        
        purified_value.Remove(_T("'")); // remove  all single quote char 
        purified_value.Remove(_T("\"")); // remove all double quote char

        return "concat('" + purified_value + "')";  // note, concat is a function which was passed in a text statement
                                                                               // it is not a c++ function
    }
}

Open in new window


note, the CString can use wide characters of type wchar_t (UTF16, called UNICODE by MS) or normal char (from ANSI code page).

you could use alternatively std::wstring and std::string if you never intend to switch from single char to wide char or reverse. the std::string and std::wstring have some additional functionality compared to CString although they are not as mighty as the c# string class.
 
Sara
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Himans GhostAuthor Commented:
Hey what will happen to the concat ? as it is not a c++ function?
0
sarabandeCommented:
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
0
Himans GhostAuthor Commented:
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.
0
sarabandeCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.