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.
C++XML

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon
sarabande

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

Open in new window


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

Sara
sarabande

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
Himans Ghost

ASKER
No it isn't working that's why i posted it i tried using this already
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
sarabande

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
Himans Ghost

ASKER
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
sarabande

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Himans Ghost

ASKER
Hey what will happen to the concat ? as it is not a c++ function?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
sarabande

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
Himans Ghost

ASKER
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.
sarabande

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy