XPath to nodes with containing single quotes

Discussion in 'XML' started by Kevin, Jan 29, 2004.

  1. Kevin

    Kevin Guest

    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.
     
    Kevin, Jan 29, 2004
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Jan 29, 2004
    #2
    1. Advertising

  3. Kevin

    Kevin Guest

    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.
     
    Kevin, Jan 30, 2004
    #3
  4. 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/
     
    Martin Honnen, Jan 30, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris
    Replies:
    1
    Views:
    13,762
    Oisin
    Mar 24, 2006
  2. girish
    Replies:
    2
    Views:
    10,326
    Peter Flynn
    Aug 6, 2004
  3. Replies:
    2
    Views:
    912
  4. th3dude
    Replies:
    0
    Views:
    452
    th3dude
    Jan 3, 2007
  5. Francis Hwang
    Replies:
    2
    Views:
    403
    Brian Candler
    Oct 21, 2004
Loading...

Share This Page