473,395 Members | 1,679 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I escape single tick marks in XPath Queries?

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
4 31171
* 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
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Leif B. Kristensen | last post by:
I'm working with a Python program to insert / update textual data into a PostgreSQL database. The text has single and double quotes in it, and I wonder: What is the easiest way to escape quotes in...
4
by: new | last post by:
Just wonder how you guys handle the single quatation marks when you write the value of a text input into SQL server. Thanks! * * * Sent via DevBuilder http://www.devbuilder.org * * * Developer...
6
by: Scott Simpson | last post by:
What is a good tool for running XPath queries on Linux? I have the O'Reilly XPath book and I'm using XPath Visualizer on Windows and that seems to work fine, but I'm looking for something on Linux....
1
by: Petr Vlcek | last post by:
Hello, I am engaded in optimization of XPath queries as a topic of my diploma thesis. Our team is developing experimental system for indexing of XML document collections. My part is to develop...
2
by: Richard L Rosenheim | last post by:
Anyone care to recommend a web site that has some really good examples of XPath queries utilizing attributes? My XML books, and the web sites that I've looked at, don't really utilize...
10
by: Martin Eyles | last post by:
Hi, I want to make a scale on a graph, by telling asp to insert empty divs of class tick, and the setting the style in css. I am trying to get a 3D effect by using colored top and bottom borders...
8
by: cerelaz | last post by:
Hi, I need to do some Xpath queries. How to add (or multiply) a values returned by a const? why this //book/price/text()*10 doesn't work? what's wrong? thanks ps I need to use a great c++...
8
by: Marina Levit [MVP] | last post by:
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: ...
3
by: musicgold | last post by:
Hi, I am new in XML. But I have done coding in VBA, C, and HTML. I am using VBA to extract data from an xml file. I use Xpathbuilder to generate Xpath queries for my work. However, some Xpath...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.