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 8957489
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:00:07+00:00 2026-06-15T15:00:07+00:00

How do you run an Xpath query on the current SimpleXMLElement object inside a

  • 0

How do you run an Xpath query on the current SimpleXMLElement object inside a for loop. For example

<?php
$xml = simplexml_load_string('<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore> ');


foreach ($xml as $value) {
    $result = $value->xpath('//title');
    var_dump($result);
    break;
}
?>

array
  0 => 
    object(SimpleXMLElement)[189]
      public '@attributes' => 
        array
          'lang' => string 'eng' (length=3)
      string 'Harry Potter' (length=12)
  1 => 
    object(SimpleXMLElement)[188]
      public '@attributes' => 
        array
          'lang' => string 'eng' (length=3)
      string 'Learning XML' (length=12)

This outputs the results of both book elements whereas it is my intent to only show the first. I am attempting for the query to run only on the $value object in the for loop as I may want to run different queries on each book (So it must be in a loop!) Not on every single book which is what it is currently doing.

  • 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-06-15T15:00:09+00:00Added an answer on June 15, 2026 at 3:00 pm

    // Selects nodes in the document from the current node that match the selection no matter where they are.

    I was already in the required node (e.g. $value) when I ran a query to select all nodes from the root, therefore gaining info from all nodes.

    Since I am already in the required node element removal the // obtains the expected result, running the query on the current node.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My XML document looks like this When I run XPATH query //collected_objects , I
I run an XPath in Java with the following xml and code: <?xml version=1.0
I've loaded a XML document, and now I wish to run a XPath query
Consider the following XSLT script: <?xml version=1.0 encoding=ISO-8859-1?> <xsl:stylesheet version=1.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform> <xsl:output method=text encoding=iso-8859-1/>
is it possible to run an SQL query, with an XPath where clause, and
I am trying to run a Perl file I created. use XML::XPath; use XML::XPath::XMLParser;
How do I run an XPath query in QT? I need to sort out
Ive got a large XML set, which I would like to run some xpath
I wrote an app that uses XPath to query some XML config files. When
Is there any way in XPath DOM Programming with using System.Xml to run selectNodes

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.