NSXMLElement & Xpath

  • Hi,

    I bet a trivial question for experts but I couldn't find an answer
    nevertheless. Given the following NSXML fragment:

    <author>
        <name>G. G. Kacprzak</name>
        <arxiv:affiliation xmlns:arxiv="http://arxiv.org/schemas/
    atom">NMSU</arxiv:affiliation>
    </author>

    With the XPath query: /author[1]/name[1]  I correctly get the name
    element
    With the XPath query: /author[1]/arxiv:affiliation[1] I expect to get
    the arxiv:affiliation element

    However, the latter won't work, even though if I ask the rootelement
    for its 2nd child and ask for its XPath, I do get /author[1]/
    arxiv:affiliation[1] !!
    The XQuery error I get returned says: can't resolve QName for
    "arxiv:affiliation"

    Can anyone tell me how to format the xpath or escape it such that I
    get the element by its name?
    I simply want to be able to do: [[xmlDoc rootElement] nodesForXPath:
    @"//arxiv:affiliation" error: err]

    Thanks!
    Alex

    **********************************************
              ** Alexander Griekspoor  PhD **
    **********************************************
              mekentosj.com

      Papers - Your Personal Library of Science
      2007 Winner of the Apple Design Awards
              Best Mac OS X Scientific Solution
              http://www.mekentosj.com/papers
    **********************************************
  • On 06/12/2007, at 10:26 AM, Alexander Griekspoor wrote:

    > I bet a trivial question for experts but I couldn't find an answer
    > nevertheless. Given the following NSXML fragment:
    >
    > <author>
    > <name>G. G. Kacprzak</name>
    > <arxiv:affiliation xmlns:arxiv="http://arxiv.org/schemas/
    > atom">NMSU</arxiv:affiliation>
    > </author>
    >
    > With the XPath query: /author[1]/name[1]  I correctly get the name
    > element
    > With the XPath query: /author[1]/arxiv:affiliation[1] I expect to
    > get the arxiv:affiliation element
    >
    > However, the latter won't work, even though if I ask the rootelement
    > for its 2nd child and ask for its XPath, I do get /author[1]/
    > arxiv:affiliation[1] !!
    > The XQuery error I get returned says: can't resolve QName for
    > "arxiv:affiliation"
    >
    > Can anyone tell me how to format the xpath or escape it such that I
    > get the element by its name?
    > I simply want to be able to do: [[xmlDoc rootElement] nodesForXPath:
    > @"//arxiv:affiliation" error: err]

    I believe this can be slightly tricky because the arxiv:affiliation
    element is not a plain element but an element with a namespace which
    is treated quite differently in XPath. You can't just treat it as an
    element with name "arxiv:affiliation", you have to deal with the
    namespace first.

    I've never had to do this myself but this article on the Microsoft
    site seems to have some good info, even if it's MS-centric:

    http://msdn2.microsoft.com/en-us/library/ms950779.aspx

    --
    Rob Keniger
  • On 6 Dec 2007, at 00:26, Alexander Griekspoor wrote:

    > I bet a trivial question for experts but I couldn't find an answer
    > nevertheless. Given the following NSXML fragment:
    >
    > <author>
    > <name>G. G. Kacprzak</name>
    > <arxiv:affiliation xmlns:arxiv="http://arxiv.org/schemas/
    > atom">NMSU</arxiv:affiliation>
    > </author>
    >
    > With the XPath query: /author[1]/name[1]  I correctly get the name
    > element
    > With the XPath query: /author[1]/arxiv:affiliation[1] I expect to
    > get the arxiv:affiliation element
    >
    > However, the latter won't work, even though if I ask the rootelement
    > for its 2nd child and ask for its XPath, I do get /author[1]/
    > arxiv:affiliation[1] !!

    The latter is a bug; that XPath would only work if the XPath engine
    was mapping the prefix arxiv to the URN http://arxiv.org/schemas/atom,
    which it clearly isn't.  Some XPath implementations provide functions
    to define prefixes; I'm not sure that Cocoa's implementation does
    though... I can't see any obvious way to do that.

    Try something like

      /author[1]/{http://arxiv.org/schemas/atom}affiliation[1]

    instead.

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
  • Thanks Alastair, the Xquery engine unfortunately reports "{" and "}"
    as invalid tokens so  /author[1]/{http://arxiv.org/schemas/atom}
    affiliation[1] won't work either.
    The only thing I could do was to resolve to iterating over the
    children of the author element and ask for their names.
    Alex

    On 6-dec-2007, at 10:41, Alastair Houghton wrote:

    > On 6 Dec 2007, at 00:26, Alexander Griekspoor wrote:
    >
    >> I bet a trivial question for experts but I couldn't find an answer
    >> nevertheless. Given the following NSXML fragment:
    >>
    >> <author>
    >> <name>G. G. Kacprzak</name>
    >> <arxiv:affiliation xmlns:arxiv="http://arxiv.org/schemas/
    >> atom">NMSU</arxiv:affiliation>
    >> </author>
    >>
    >> With the XPath query: /author[1]/name[1]  I correctly get the name
    >> element
    >> With the XPath query: /author[1]/arxiv:affiliation[1] I expect to
    >> get the arxiv:affiliation element
    >>
    >> However, the latter won't work, even though if I ask the
    >> rootelement for its 2nd child and ask for its XPath, I do get /
    >> author[1]/arxiv:affiliation[1] !!
    >
    > The latter is a bug; that XPath would only work if the XPath engine
    > was mapping the prefix arxiv to the URN http://arxiv.org/schemas/
    > atom, which it clearly isn't.  Some XPath implementations provide
    > functions to define prefixes; I'm not sure that Cocoa's
    > implementation does though... I can't see any obvious way to do that.
    >
    > Try something like
    >
    > /author[1]/{http://arxiv.org/schemas/atom}affiliation[1]
    >
    > instead.
    >
    > Kind regards,
    >
    > Alastair.
    >
    > --
    > http://alastairs-place.net
    >
    >

    **********************************************
              ** Alexander Griekspoor  PhD **
    **********************************************
              mekentosj.com

      Papers - Your Personal Library of Science
      2007 Winner of the Apple Design Awards
              Best Mac OS X Scientific Solution
              http://www.mekentosj.com/papers
    **********************************************
  • On 6 Dec 2007, at 11:24, Alexander Griekspoor wrote:

    > Thanks Alastair, the Xquery engine unfortunately reports "{" and "}"
    > as invalid tokens so  /author[1]/{http://arxiv.org/schemas/
    > atom}affiliation[1] won't work either.
    > The only thing I could do was to resolve to iterating over the
    > children of the author element and ask for their names.

    Hmmm.  That's a shame (and arguably a bug, since it makes the Cocoa
    XPath implementation incompatible with most others).  The {}
    "universal name" syntax isn't specified in the XPath standard, but it
    is common to almost all implementations and was defined by James Clark
    (the same chap who wrote the standard).

    Kind regards,

    Alastair.

    --
    http://alastairs-place.net
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today