473,385 Members | 1,764 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,385 software developers and data experts.

XPath to nodes with containing single quotes

I know this has probably been discussed many times before (I found
answers when I searched yesterday), but I still can't get it to
work...

I have an attribute @OID that can contain any characters (including
" and ')

I have java code using Xerces to select a node based on it's @OID
attribute using an XPath like this:

"/MyElement[@OID = '" + sOID + "']" (done from memory - it's not
my code)

My first thought was that sOID should have all it's special characters
escaped (e.g. "l'hotel"), but this did not work - always
returning me null.

I read about the problem on the web and all the answers seemed to say
I was doing the right thing. So, I tried typing the XPath into
XMLSpy's XPath module, and it returned me nothing as well.

Am I missing something, or should this actually be working?
To test it, I created nodes with OID="<" OID=""" OID="'"
OID="&". After some experimentation it appears that if the "sOID"
variable contains non-escaped characters, the only node I cannot get
returned is OID="'", and if sOID contains escaped characters, I
cannot get any of these nodes returned.

Many thanks in advance, because this is driving me a bit crazy.
Jul 20 '05 #1
3 5794


Kevin wrote:
I know this has probably been discussed many times before (I found
answers when I searched yesterday), but I still can't get it to
work...

I have an attribute @OID that can contain any characters (including
" and ')

I have java code using Xerces to select a node based on it's @OID
attribute using an XPath like this:

"/MyElement[@OID = '" + sOID + "']" (done from memory - it's not
my code)

My first thought was that sOID should have all it's special characters
escaped (e.g. "l'hotel"), but this did not work - always
returning me null.


If you are writing XML (for instance an XSLT stylesheet with XPath
expressions) then entity references like ' are helpful but if you
are having XPath expressions as string values in a Java program then
they don't help.
With Java string literals are delimited by double quotes, with XPath you
can use single or double quotes as needed so as long as you know your
XPath string value contains only single quotes you could use
"/MyElement[@OID = \"" + sOID + "\"]"
in Java. In general if your API to XPath doesn't allow you to declare
and intitialize variables to be used in expressions you will need to
split up string values into XPath concat calls where you split up the
string into components containing only a single or double quotes.

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Thanks for the response.
As I said in the proginal posting, the string can contain anything at
all unfortunately. The second solution (breaking it up) is something I
had thought of, but I was hoping not to have to do.

Is it the case that
"/MyElement[@OID = 'l'Hotel']"
is not actually valid XPath? Or is it looking for the characters & a p
o s ; in a row together?

My simple mind tells me that this XPath ought to work, and this is
what I have seen recommended elsewhere on the web, but it doesn't seem
to work in our Java code or with XMLSpy's XPath engine.
With Java string literals are delimited by double quotes, with XPath you
can use single or double quotes as needed so as long as you know your
XPath string value contains only single quotes you could use
"/MyElement[@OID = \"" + sOID + "\"]"
in Java. In general if your API to XPath doesn't allow you to declare
and intitialize variables to be used in expressions you will need to
split up string values into XPath concat calls where you split up the
string into components containing only a single or double quotes.

Jul 20 '05 #3


Kevin wrote:
As I said in the proginal posting, the string can contain anything at
all unfortunately. The second solution (breaking it up) is something I
had thought of, but I was hoping not to have to do.

Is it the case that
"/MyElement[@OID = 'l'Hotel']"
is not actually valid XPath? Or is it looking for the characters & a p
o s ; in a row together?


As said, that depends on the context, if that is inside an XML file
parsed by an XML parser the parser will recognize the entitity reference
' and hand on the character ' to whatever processes the XPath.
But if it is a pure XPath expression evaluator then ' is indeed a
sequence of six characters.


--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
9
by: Iain | last post by:
I want to create an XML configuration file which might look like <REGION Name="Europe" WingDing="Blue"> <COUNTRY Name="UK" WingDing="white"> <TOWN Name="London" WingDing="Orange" /> </COUNTRY>...
2
by: aman.coe | last post by:
hello all Consider this xml file: <root> <body> <ElementX id="X1" Xproperty="X_Default_Value" /> <ElementX id="X2" /> </body> </root>
2
by: Joe Gass | last post by:
I have an xml doc <sections> <section> <title>entry image</title> </section> <section> <title>22'2</title> </section> </sections>
7
by: Ot | last post by:
I posted this to the wrong group. It went to m.p.dotnet.languages.vb. Ooops. -------------------------------------------------------------------- I have this tiny problem. I have learned...
4
by: Greg | last post by:
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">...
13
by: David Thielen | last post by:
XPathNavigator nav = MyCreateNav(); // InnerXml == "software" nav.SelectSingleNode"."); The select returns an exception: + $exception {"'.' has an invalid token."} System.Exception...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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...

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.