XPath to nodes with containing single quotes

K

Kevin

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

Martin Honnen

Kevin said:
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.
 
K

Kevin

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

Martin Honnen

Kevin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top