Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > XPath to nodes with containing single quotes

Reply
Thread Tools

XPath to nodes with containing single quotes

 
 
Kevin
Guest
Posts: n/a
 
      01-29-2004
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 &apos

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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-29-2004


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 &apos
>
> 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/

 
Reply With Quote
 
 
 
 
Kevin
Guest
Posts: n/a
 
      01-30-2004
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.

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-30-2004


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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dataset Question - Odd results with single xml nodes vs multiple nodes th3dude ASP .Net 0 01-03-2007 03:24 PM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
Should Xpath select nodes containing default values?? aman.coe@gmail.com XML 2 10-22-2005 03:30 AM
Escaping single quotes in XPath query with REXML Francis Hwang Ruby 2 10-21-2004 08:36 AM
XPath for attributes with single and double quotes girish XML 2 08-06-2004 07:12 PM



Advertisments