473,561 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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'Don nel">
<Author LastName="Smith ">
</Authors>

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

If I change it to
SelectNodes(Aut hor/Author[@LastName="O'Do nnel"]) 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 31198
* Greg wrote in microsoft.publi c.dotnet.xml:
If I change it to
SelectNodes(Au thor/Author[@LastName="O'Do nnel"]) 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****@h oehrmann.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****@hoehrma nn.de> wrote in message
news:41******** ********@news.b joern.hoehrmann .de...
* Greg wrote in microsoft.publi c.dotnet.xml:
If I change it to
SelectNodes(Au thor/Author[@LastName="O'Do nnel"]) 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****@h oehrmann.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*****@marlab s.com> wrote in message
news:%2******** ********@TK2MSF TNGP12.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
22746
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 Python, similar to the Perlism "$str =~ s/()/\\$1/g;"? I tried the re.escape() method, but it escapes far too much, including spaces and accented...
4
3500
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 Resources for High End Developers.
6
8291
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. I've tried Quanta. It seems to be able to run the xsltproc processor to do XSLT transformations but it doesn't seem to cope with interactively...
1
1280
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 simple XPath query optimizer that would use indices and maybe XML schemata. I am looking for some sources and I am not sure where should I get...
2
2239
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 attributes. So, their samples on the subject is weak. I tried looking at a number of sites based upon a Google query, but didn't find any that was that...
10
2179
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 on the divs, and setting their height to 0. The code bellow show an example tick, and it all looks beautiful in firefox, but in internet explorer,...
8
1661
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++ library(*nix) with nice documentation. Any
8
21473
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: "SomeNode[SomeAttribute = 'abc@apos;s search'"
3
5092
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 queries are causing errors in my program. As I am new in xml and its standards I am not sure what is causing them. Answers to the following questions...
0
7644
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7568
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7614
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1180
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
892
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.