Get the names of children using XPATH

Discussion in 'XML' started by cmsaunders@gmail.com, Feb 12, 2008.

  1. Guest

    I am using simpleXML in PHP with an XML file that looks something
    like:

    <people>
    <person>
    <personID>001</personID>
    <forename>Jo</forename>
    <surname>Bloggs</surname>
    </person>
    </people>

    I understand that to select the above person, I use:
    /people/person[personID='001']

    What I want to do is, "give me the names of all elements for the
    person with personID 001". This would then return, 'forename and
    'surname'. It may be assumed that I do not know that 'forename' and
    'surname' exist and there may be other things in there such as
    'eyeColour'.

    Due to the PHP version being run, I can not use simpleXML's getName()
    function. Additionally, XSLT is not an alternative.

    Any ideas?

    Regards.
    , Feb 12, 2008
    #1
    1. Advertising

  2. wrote:
    > I am using simpleXML in PHP with an XML file that looks something
    > like:
    >
    > <people>
    > <person>
    > <personID>001</personID>
    > <forename>Jo</forename>
    > <surname>Bloggs</surname>
    > </person>
    > </people>
    >
    > I understand that to select the above person, I use:
    > /people/person[personID='001']
    >
    > What I want to do is, "give me the names of all elements for the
    > person with personID 001". This would then return, 'forename and
    > 'surname'. It may be assumed that I do not know that 'forename' and
    > 'surname' exist and there may be other things in there such as
    > 'eyeColour'.


    Use an XPath alike
    /people/person[personID = "001"]/*[not(self::personID)]
    then iterate over the result and access the nodeName property of each node.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 12, 2008
    #2
    1. Advertising

  3. Guest

    > Use an XPath alike
    > /people/person[personID = "001"]/*[not(self::personID)]
    > then iterate over the result and access the nodeName property of each node.


    Doesn't XPath have anything that can do this itself?
    , Feb 12, 2008
    #3
  4. wrote:
    >> Use an XPath alike
    >> /people/person[personID = "001"]/*[not(self::personID)]
    >> then iterate over the result and access the nodeName property of each node.

    >
    > Doesn't XPath have anything that can do this itself?


    Well
    name(/people/person[personID = "001"]/*[not(self::personID)])
    would give you a string with the name of the first child element not
    being personID but I don't see how that would help if you want the name
    of several child elements. You would still need to iterate over the
    results of an expression like the one given in my first answer, then, if
    you somehow don't want to use the nodeName property, you could evaluate
    name(.) with each element as the context node.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 12, 2008
    #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. Jeff
    Replies:
    2
    Views:
    1,068
    Dimitre Novatchev
    Jul 5, 2003
  2. Hannes Heckner
    Replies:
    1
    Views:
    764
    Martin Honnen
    Oct 13, 2003
  3. Michael Reiche
    Replies:
    3
    Views:
    10,846
    Michael Reiche
    Feb 5, 2004
  4. Matt
    Replies:
    2
    Views:
    847
    Ben Edgington
    Oct 12, 2004
  5. Arndt Jonasson
    Replies:
    6
    Views:
    537
    Dimitre Novatchev
    Sep 7, 2008
Loading...

Share This Page