Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 638191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:42:20+00:00 2026-05-13T20:42:20+00:00

Using PHP, SimpleXML and the xpath() function, I want to select a child node,

  • 0

Using PHP, SimpleXML and the xpath() function, I want to select a child node, starting from a certain point.
Can you please help me? I know in this specific case I could use an expression starting with “//”, but I want to learn the right way of selecting a child node.
Thanks.

First try — fails with Undefined offset: 0 error

$navXmlObject = simplexml_load_file("main_navigation.xml");
$tmpObject = $navXmlObject->website->xpath('title[@lang="fr"]'); 
echo($tmpObject[0]["label"]);

Second try, adding a slash — also fails with the same error

$tmpObject = $navXmlObject->website->xpath('/title[@lang="fr"]'); 

XML file

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <website id="MainWeb">
        <title lang="fr" label="Mon site Web" />        
        <title lang="en" label="My web site" />     
        <menuNodes>

            <menuNode id="Home">
                <menuNodeData lang="fr" label="Accueil" url="/fr/accueil/" />
                <menuNodeData lang="en" label="Home" url="/en/home/" />
            </menuNode>

            <menuNode id="Prod">

                <menuNodeData lang="fr" label="Produits" url="/fr/produits/" />
                <menuNodeData lang="en" label="Products" url="/en/products/" />

                <menuNode id="Shoe">
                    <menuNodeData lang="fr" label="Chaussures" url="/fr/produits/chaussures/" />
                    <menuNodeData lang="en" label="Shoes" url="/en/products/shoes/" />
                </menuNode>

            </menuNode>

            <menuNode id="Biog">
                <menuNodeData lang="fr" label="Biographie du fondateur" url="/fr/biographie/" />
                <menuNodeData lang="en" label="Biography of founder" url="/en/biography/" />
            </menuNode>

        </menuNodes>
    </website>
</root>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T20:42:20+00:00Added an answer on May 13, 2026 at 8:42 pm

    It is strongly advised that you upgrade to a recent stable build and enjoy all of the improvements that would come with it. Now that that’s over with, the reason why your code is not working is precisely because such an old version of SimpleXML does not behave exactly as you expect it to.

    The context node for the queries is not where you expect it to be (compared to saner, later versions for example). For the sake of ease of understanding, in my opinion, it would be easier for you just to use an absolute XPath.

    Some examples that will work in your PHP 5.0.5 (and do in current versions) are:

    $xmlObject->xpath("/root/website/title[@lang='fr']")
    $xmlObject->xpath("./website/title[@lang='fr']")
    $xmlObject->xpath("//website/title[@lang='fr']")
    $xmlObject->xpath("website/title[@lang='fr']")
    

    To throw a spanner in the works, here’s an example that is just silly (yet should work for you in 5.0.5):

    $xmlObject->website->menuNodes->xpath("title[@lang='fr']")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can't know the exact server time from from javascript(unless… May 16, 2026 at 12:35 pm
  • Editorial Team
    Editorial Team added an answer If you want this library to be a direct replacement… May 16, 2026 at 12:35 pm
  • Editorial Team
    Editorial Team added an answer phpMyAdmin is not a database, but a front-end for a… May 16, 2026 at 12:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm using SimpleXML and xpath to read elements from an external UTF-8 XHTML document.
How can I just obtain the string value of a Xml node using xpath
I am using a PHP function that will return 2 arrays. Below is my
Hi Im trying to parse an xml feed using simplexml in php. The xml
Using PHP I'm attempting to take an HTML string passed from a WYSIWYG editor
i am writing a site from scratch using php/mysql and am using GET requests
I want split a string using PHP to get a individual date info. For
I am parsing an xml file using php simplexml_load_file() and then inserting the desired
I've been reading about XML, XSL, XPath, etc. I want to start this small
Using PHP, I'm trying to give each specific text its own variable. I believe

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.