By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,394 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

How to escape single quotes

P: n/a
I've scoured google, but apparently none of the suggestions actually work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"

and I've tried

"SomeNode[SomeAttribute = 'abc@quot;s search'"

And neither one of these gives me a result.

Note, because of the structure and the way this XPATH is put together, I
would prefer to not have to use double quotes as the delimiters for the
search string, I'd like to keep it single quotes.

How to make this query find my node?
Nov 13 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Marina Levit [MVP] wrote:
I've scoured google, but apparently none of the suggestions actually work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"
Assuming you actually meant '...

<xsl:variable name="apos">
<xsl:text>'</xsl:text>
</xsl:variable>
<xsl:if test="SomeNode[SomeAttribute = concat('abc',$apos,'s search')]">
....

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Nov 13 '06 #2

P: n/a
But I have an XPATH expression and I am searching for a node. How does what
you wrote fit into that?

"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4r************@mid.individual.net...
Marina Levit [MVP] wrote:
>I've scoured google, but apparently none of the suggestions actually
work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"

Assuming you actually meant &apos;...

<xsl:variable name="apos">
<xsl:text>'</xsl:text>
</xsl:variable>
<xsl:if test="SomeNode[SomeAttribute = concat('abc',$apos,'s search')]">
...

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Nov 14 '06 #3

P: n/a
Marina Levit [MVP] wrote:
I've scoured google, but apparently none of the suggestions actually work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"

and I've tried

"SomeNode[SomeAttribute = 'abc@quot;s search'"

And neither one of these gives me a result.

Note, because of the structure and the way this XPATH is put together, I
would prefer to not have to use double quotes as the delimiters for the
search string, I'd like to keep it single quotes.

How to make this query find my node?
In what context are you using the XPath? If that is an XSLT stylesheet
then using entity references like &apos; or &quot; might help but if you
have .NET code (e.g. C# or VB.NET) then using those XML entity
references is not possible.
As for that escape problem, it is ugly, XPath 1.0 itself does not have
any escape mechanism so you need to split strings e.g. C# string literal
to be passed to SelectSingleNode looks like
@"SomeNode[@SomeAttribute = concat('abc', ""'"", 's search')]"

See also Oleg's blog
<http://www.tkachenko.com/blog/archives/000627.htmlrespectively the
XPathCache API the MVP XML project provides.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 14 '06 #4

P: n/a
Oy... what a headache.

The problem with this approach is that I can't just escape the characters.
There is a ton of code that already assumes there are single quote
delimiters to the search string. All that would have to be changed.

Using the jscript XML object model this all works without a problem. And
yet, .NET can't implement an XML DOM that works half way decent.

Thanks

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:e%****************@TK2MSFTNGP03.phx.gbl...
Marina Levit [MVP] wrote:
>I've scoured google, but apparently none of the suggestions actually
work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"

and I've tried

"SomeNode[SomeAttribute = 'abc@quot;s search'"

And neither one of these gives me a result.

Note, because of the structure and the way this XPATH is put together, I
would prefer to not have to use double quotes as the delimiters for the
search string, I'd like to keep it single quotes.

How to make this query find my node?

In what context are you using the XPath? If that is an XSLT stylesheet
then using entity references like &apos; or &quot; might help but if you
have .NET code (e.g. C# or VB.NET) then using those XML entity references
is not possible.
As for that escape problem, it is ugly, XPath 1.0 itself does not have any
escape mechanism so you need to split strings e.g. C# string literal to be
passed to SelectSingleNode looks like
@"SomeNode[@SomeAttribute = concat('abc', ""'"", 's search')]"

See also Oleg's blog
<http://www.tkachenko.com/blog/archives/000627.htmlrespectively the
XPathCache API the MVP XML project provides.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 14 '06 #5

P: n/a
Marina Levit [MVP] wrote:
Using the jscript XML object model this all works without a problem. And
yet, .NET can't implement an XML DOM that works half way decent.
I am not sure why you think it is a .NET issue. What exactly are you
doing with JScript and MSXML that you can't do with .NET and C# (or VB.NET)?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 14 '06 #6

P: n/a
Querying a dom to locate certain nodes where a string literal has a single
quote in it.

This works perfectly in MSXML, as apparently that uses an XSL pattern query
(not that I really know exactly what that is), which is apparently more
flexible, and allows this to actually work.

..NET follows the XPATH 1.0 standard, which apparently isn't as useful,
meaning I can't have single quotes in literals when searching for nodes in a
DOM.

"Martin Honnen" <ma*******@yahoo.dewrote in message
news:eF****************@TK2MSFTNGP03.phx.gbl...
Marina Levit [MVP] wrote:
>Using the jscript XML object model this all works without a problem. And
yet, .NET can't implement an XML DOM that works half way decent.

I am not sure why you think it is a .NET issue. What exactly are you doing
with JScript and MSXML that you can't do with .NET and C# (or VB.NET)?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 14 '06 #7

P: n/a
Marina Levit [MVP] wrote:
But I have an XPATH expression and I am searching for a node. How does what
you wrote fit into that?
What I wrote. I just copied your sample XPath expression.
I'm not sure I understand what the difficulty is.

///Peter
--
XML FAQ: http://xml.silmaril.ie/

"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4r************@mid.individual.net...
>Marina Levit [MVP] wrote:
>>I've scoured google, but apparently none of the suggestions actually
work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"
Assuming you actually meant &apos;...

<xsl:variable name="apos">
<xsl:text>'</xsl:text>
</xsl:variable>
<xsl:if test="SomeNode[SomeAttribute = concat('abc',$apos,'s search')]">
...

///Peter
--
XML FAQ: http://xml.silmaril.ie/

Nov 14 '06 #8

P: n/a
You want me to use the XSL as an XPATH expression?

I think concat on its own works fine. The problem is, like I said, I didn't
want to have to change the delimiters which are single quotes everywhere.
Which I would have to do, so the concat doesn't end up as a literal.

I expected to be able to do a replace in the search string to escape the
single quotes, so single quotes can remain the delimiters for the literal.

"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4r************@mid.individual.net...
Marina Levit [MVP] wrote:
>But I have an XPATH expression and I am searching for a node. How does
what you wrote fit into that?

What I wrote. I just copied your sample XPath expression.
I'm not sure I understand what the difficulty is.

///Peter
--
XML FAQ: http://xml.silmaril.ie/

>"Peter Flynn" <pe********@m.silmaril.iewrote in message
news:4r************@mid.individual.net...
>>Marina Levit [MVP] wrote:
I've scoured google, but apparently none of the suggestions actually
work.

I have the following. type of XPATH query

"SomeNode[SomeAttribute = 'abc's search'"

Now, I've tried doing this:

"SomeNode[SomeAttribute = 'abc@apos;s search'"
Assuming you actually meant &apos;...

<xsl:variable name="apos">
<xsl:text>'</xsl:text>
</xsl:variable>
<xsl:if test="SomeNode[SomeAttribute = concat('abc',$apos,'s search')]">
...

///Peter
--
XML FAQ: http://xml.silmaril.ie/
Nov 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.