By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,070 Members | 1,237 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,070 IT Pros & Developers. It's quick & easy.

XPath to nodes with containing single quotes

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a


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

P: n/a
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

P: n/a


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 discussion thread is closed

Replies have been disabled for this discussion.