PHP/XPath: Iterating through ancestor nodes in XML using SimpleXML

Posted by StackOverflowNewbie on Stack Overflow See other posts from Stack Overflow or by StackOverflowNewbie
Published on 2011-01-08T06:35:27Z Indexed on 2011/01/08 6:53 UTC
Read the original article Hit count: 257

Filed under:
|
|

I need to loop through the Ancestors nodes in order to get corresponding BrowseNodeId and Name values. How do I do this using XPath? I'm trying something like the the following, but it's not working:

//BrowseNode[1]//Ancestors[1]/BrowseNode/BrowseNodeId/text()
//BrowseNode[1]//Ancestors[1]/BrowseNode/Name/text()

I think the //Ancestors part of the XPath is actually searching the entire XML tree. What I need to do is visit each node to get the needed values. How do I do this? In the example below, I want: Europe -> History -> Subjects -> Books.

Example XML:

    <BrowseNode>
        <BrowseNodeId>4952</BrowseNodeId>
        <Name>France</Name>
        <Ancestors>
            <BrowseNode>
                <BrowseNodeId>4935</BrowseNodeId>
                <Name>Europe</Name>
                <Ancestors>
                    <BrowseNode>
                        <BrowseNodeId>9</BrowseNodeId>
                        <Name>History</Name>
                        <Ancestors>
                            <BrowseNode>
                                <BrowseNodeId>1000</BrowseNodeId>
                                <Name>Subjects</Name>
                                <IsCategoryRoot>1</IsCategoryRoot>
                                <Ancestors>
                                    <BrowseNode>
                                        <BrowseNodeId>283155</BrowseNodeId>
                                        <Name>Books</Name>
                                    </BrowseNode>
                                </Ancestors>
                            </BrowseNode>
                        </Ancestors>
                    </BrowseNode>
                </Ancestors>
            </BrowseNode>
        </Ancestors>
    </BrowseNode>

© Stack Overflow or respective owner

Related posts about php

Related posts about Xml