all groups > dotnet xml > december 2004 >
You're in the

dotnet xml

group:

How do I escape single tick marks in XPath Queries?



How do I escape single tick marks in XPath Queries? Greg
12/29/2004 9:00:15 AM
dotnet xml: 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

Re: How do I escape single tick marks in XPath Queries? Greg
12/29/2004 9:27:57 AM
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.


[quoted text, click to view]

Re: How do I escape single tick marks in XPath Queries? Mujtaba Syed
12/29/2004 10:43:26 AM
Hi Greg:

[quoted text, click to view]

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

[quoted text, click to view]

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.

Re: How do I escape single tick marks in XPath Queries? Greg
12/29/2004 2:16:06 PM

[quoted text, click to view]

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.


[quoted text, click to view]

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

[quoted text, click to view]

Re: How do I escape single tick marks in XPath Queries? Bjoern Hoehrmann
12/29/2004 3:14:58 PM
* Greg wrote in microsoft.public.dotnet.xml:
[quoted text, click to view]

That's not possible, all you could do is to have a variable or function
that resolves to the desired character (or string).

[quoted text, click to view]

http://www.w3.org/TR/xpath should answer all XPath 1.0 questions.
--
Björn Höhrmann · mailto:bjoern@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
AddThis Social Bookmark Button