XML XPath question

  • Greetings.

    I am trying to retrieve some specific data from a XML document.

    <data>
    <department>
      <employe id="1">
      <name>abcd</name>
      </employe>
      <employe id="2">
      <name>efrh</name>
      </employe>
      <employe id="3">
      <name>ijkl</name>
      </employe>
      <employe id="4">
      <name>mnop</name>
      </employe>
    </department>
    </data>

    so I'm trying to query the XML document with the following XPath string.

    [... stringWithFormat:@".//data/department/employe[id=\"%@\"]",
    [employeID stringValue]]

    this returns no objects.

    if i try the same line without th attribute in the query
    [".//data/department/employe"]

    I receive an array with all the employe records, witch is the expected
    behavior.

    what am I doing wrong in the first query, the documentation says this.
    .//part/chapter[1]/section[title="Path Expressions"]
    The evaluation of a path expression works from left to right. This
    expression first gets all the elements named part and from that
    selects the first element named chapter; from that it gets all child
    elements named section, and from that sequence it returns the section
    element whose title is “Path Expressions”. XPath also lets you find
    attributes by name by using an at-sign (@) prefix. For example, the
    following path expression gets the modification date (an attribute) of
    the first chapter:

    Thank you any help is appreciated.

    Sandro.
  • > I am trying to retrieve some specific data from a XML document.
    >
    > <data>
    > <department>
    > <employe id="1">
    > <name>abcd</name>
    > </employe>
    > <employe id="2">
    > <name>efrh</name>
    > </employe>
    > <employe id="3">
    > <name>ijkl</name>
    > </employe>
    > <employe id="4">
    > <name>mnop</name>
    > </employe>
    > </department>
    > </data>
    >
    > so I'm trying to query the XML document with the following XPath
    > string.
    >
    > [... stringWithFormat:@".//data/department/employe[id=\"%@\"]",
    > [employeID stringValue]]
    >
    > this returns no objects.

    Your XPath is almost correct, as far as I can tell. All you need to do
    is insert an "@" character before the "id" attribute predicate, like so:

    ".//data/department/employe[@id=\"%@\"]"

    ================================================================
    Chris Patterson      chris dot s dot patterson at gmail dot com
    "The only problem with Microsoft is they just have no taste."
      -- Steve Jobs <http://www.youtube.com/watch?v=WfALGcDNEDw>
    ================================================================
  • Chris,
    thank you that worked just fine, to bad the piece of information i
    read did not mention that... :(

    Sandro.

    On 1-Oct-08, at 12:47 PM, Chris Patterson wrote:
    >
    > Your XPath is almost correct, as far as I can tell. All you need to
    > do is insert an "@" character before the "id" attribute predicate,
    > like so:
    >
    > ".//data/department/employe[@id=\"%@\"]"
    >
    > ================================================================
    > Chris Patterson      chris dot s dot patterson at gmail dot com
    > "The only problem with Microsoft is they just have no taste."
    > -- Steve Jobs <http://www.youtube.com/watch?v=WfALGcDNEDw>
    > ================================================================