Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Get the names of children using XPATH

Reply
Thread Tools

Get the names of children using XPATH

 
 
cmsaunders@gmail.com
Guest
Posts: n/a
 
      02-12-2008
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      02-12-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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:ersonID)]
then iterate over the result and access the nodeName property of each node.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
cmsaunders@gmail.com
Guest
Posts: n/a
 
      02-12-2008
> Use an XPath alike
> /people/person[personID = "001"]/*[not(self:ersonID)]
> then iterate over the result and access the nodeName property of each node.


Doesn't XPath have anything that can do this itself?
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      02-12-2008
(E-Mail Removed) wrote:
>> Use an XPath alike
>> /people/person[personID = "001"]/*[not(self:ersonID)]
>> 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:ersonID)])
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/
 
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
Distinguish between empty string and no children, in XPath 2? Arndt Jonasson XML 6 09-07-2008 04:48 AM
XSLT Select nodes without text-node children whose names starts with specifix text Michael Reiche XML 3 02-05-2004 10:40 PM
XPath: How to select all nodes which don't have children` Hannes Heckner XML 1 10-13-2003 09:43 AM
XPath expression that gets a node with 2 particular children Jeff XML 2 07-05-2003 06:20 PM



Advertisments