Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / December 2004

Tip: Looking for answers? Try searching our database.

How do I escape single tick marks in XPath Queries?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg - 29 Dec 2004 14:00 GMT
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
Bjoern Hoehrmann - 29 Dec 2004 14:14 GMT
* 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.
Signature

Björn Höhrmann · mailto:bjoern@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/

Greg - 29 Dec 2004 14:27 GMT
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.

> * Greg wrote in microsoft.public.dotnet.xml:
> >If I change it to
[quoted text clipped - 9 lines]
>
> http://www.w3.org/TR/xpath should answer all XPath 1.0 questions.
Mujtaba Syed - 29 Dec 2004 15:43 GMT
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.
Greg - 29 Dec 2004 19:16 GMT
> Hi Greg:
>
[quoted text clipped - 13 lines]
> (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
[quoted text clipped - 4 lines]
> an XML Document as specified by the XML DOM. It does not need to store
> attribute enclosed within quotes.

> Thanks,
> Mujtaba.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.