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

How do I escape single tick marks in XPath Queries?

P: n/a
I keep getting an error when I have a tick mark in a text value that I am
searching for in my XPath Query.

Example:

<Authors>
<Author LastName="O'Donnel">
<Author LastName="Smith">
</Authors>

If I try to do a SelectNodes(Authors/Author[@LastName='O'Donnel']) I get an
exception "has an invalid token".

If I change it to
SelectNodes(Author/Author[@LastName="O'Donnel"]) that works fine (and
would seem reasonable since you can't have a " in your XML legally), but I'd
rather escape the tick mark. In SQL you just put a double tick ('') but it
doesn't like that.

Googling didn't seem to find any good results... Seems like this would be a
pretty common issue, too!

Thanks,
Greg
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* Greg wrote in microsoft.public.dotnet.xml:
If I change it to
SelectNodes(Author/Author[@LastName="O'Donnel"]) that works fine (and
would seem reasonable since you can't have a " in your XML legally), but I'd
rather escape the tick mark.
That's not possible, all you could do is to have a variable or function
that resolves to the desired character (or string).
Googling didn't seem to find any good results... Seems like this would be a
pretty common issue, too!


http://www.w3.org/TR/xpath should answer all XPath 1.0 questions.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Nov 12 '05 #2

P: n/a
Thanks for the link. I read through the spec and found this to be helpful:

"To avoid a quotation mark in an expression being interpreted by the XML
processor as terminating the attribute value the quotation mark can be
entered as a character reference (&quot; or &apos;). Alternatively, the
expression can use single quotation marks if the XML attribute is delimited
with double quotation marks or vice-versa."

Interestingly, the XmlDocument object is letting me put whatever I want in
an attribute, including tick marks and double quotes. It'd be nice to see
it auto encode / decode since those aren't legal characters, or better yet
an exception thrown if I try to put them in.
"Bjoern Hoehrmann" <bj****@hoehrmann.de> wrote in message
news:41****************@news.bjoern.hoehrmann.de.. .
* Greg wrote in microsoft.public.dotnet.xml:
If I change it to
SelectNodes(Author/Author[@LastName="O'Donnel"]) that works fine (and
would seem reasonable since you can't have a " in your XML legally), but I'drather escape the tick mark.


That's not possible, all you could do is to have a variable or function
that resolves to the desired character (or string).
Googling didn't seem to find any good results... Seems like this would be apretty common issue, too!


http://www.w3.org/TR/xpath should answer all XPath 1.0 questions.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

Nov 12 '05 #3

P: n/a
Hi Greg:
"To avoid a quotation mark in an expression being interpreted by the XML
processor as terminating the attribute value the quotation mark can be
entered as a character reference (&quot; or &apos;). Alternatively, the
expression can use single quotation marks if the XML attribute is delimited with double quotation marks or vice-versa."
A well formed XML document should have all its attributes enclosed within
matching quotes, either single (') or double (").
So, <root val='Greg' /> and <root val="Greg" /> are well-formed while <root
val='Greg" /> and <root val="Greg' /> and <root val=Greg /> are not.

If you have single or double quotes are part of an attribute's value
(PCDATA), you could use the alternate quote to enclose attributes.
Ex, <root val="Greg's car" /> and <root val='Greg's car' />

A better way is to escape using a built in entity reference:

&quot; - to produce the " character
&apos; - to produce the ' character
Interestingly, the XmlDocument object is letting me put whatever I want in
an attribute, including tick marks and double quotes. It'd be nice to see
it auto encode / decode since those aren't legal characters, or better yet
an exception thrown if I try to put them in.


The XMLDocument is a framework type that is the in-memory representation of
an XML Document as specified by the XML DOM. It does not need to store
attribute enclosed within quotes.

Thanks,
Mujtaba.
Nov 12 '05 #4

P: n/a

"Mujtaba Syed" <mu*****@marlabs.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hi Greg:
"To avoid a quotation mark in an expression being interpreted by the XML
processor as terminating the attribute value the quotation mark can be
entered as a character reference (&quot; or &apos;). Alternatively, the
expression can use single quotation marks if the XML attribute is delimited
with double quotation marks or vice-versa."


A well formed XML document should have all its attributes enclosed within
matching quotes, either single (') or double (").
So, <root val='Greg' /> and <root val="Greg" /> are well-formed while

<root val='Greg" /> and <root val="Greg' /> and <root val=Greg /> are not.

If you have single or double quotes are part of an attribute's value
(PCDATA), you could use the alternate quote to enclose attributes.
Ex, <root val="Greg's car" /> and <root val='Greg's car' />
If you read the recommendation paragraph quote (earlier in this thread)
you'll see that a tick mark is not valid in an attribute unless it is
enclosed with double qoutes (ie "Greg's car" is valid, while 'Greg's car'
is not). Interestingly, if I try doing something like

"Greg's car" is great as the attribute , the XML Dom tells me I have an
invalid character when I try to load the doucment. In other words, double
quotes are not valid inside an attribute, but single quotes are. If I am
using single quotes in my XPath query as the beginning/ending expression, I
get an error if a single quote exists in my query. I find this behavior a
little inconsistent in the DOM and that was more my gripe than anything.


A better way is to escape using a built in entity reference:

&quot; - to produce the " character
&apos; - to produce the ' character
This is true, BUT you will still have to do the decoding / encoding
manually. In other words, if I want to search for "Greg's car" with XPath, I
would have to convert my query to Greg&quot;s Car first. I was simply
saying that it would be nice if this conversion could be done automatically
by the DOM. Of course, it may work fine in my situation, but there are
probably other situations that it wouldn't work all that great for...
Interestingly, the XmlDocument object is letting me put whatever I want in an attribute, including tick marks and double quotes. It'd be nice to see it auto encode / decode since those aren't legal characters, or better yet an exception thrown if I try to put them in.
The XMLDocument is a framework type that is the in-memory representation

of an XML Document as specified by the XML DOM. It does not need to store
attribute enclosed within quotes.
Thanks,
Mujtaba.

Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.