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

  • SEARCH
  • Home
  • 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 6538451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:44:14+00:00 2026-05-25T10:44:14+00:00

Please forgive if the following is a bit muddled, I’ve been killing myself trying

  • 0

Please forgive if the following is a bit muddled, I’ve been killing myself trying to work this out.

This is a chunk of XML (exported from a much large site) that I am using to create a category tree for a mini-CMS. Once I’ve got the value and name of the node, which is no problem, I also need to get the ‘parent’ each node, that is, the node preceding it which is above it in the hierarchy.

    <productCategory>
    <genericName>DigitalCinema</genericName>
    <productCategories>
      <productCategory>
        <genericName>DCinemaProj</genericName>
        <productModels>
          <productModel>ProjProd-1</productModel>
          <productModel>ProjProd-2</productModel>
          <productModel>ProjProd-3</productModel>
          <productModel>ProjProd-4</productModel>
        </productModels>
      </productCategory>
      <productCategory>
        <genericName>DCinemaLens</genericName>
      </productCategory>
    </productCategories>
  </productCategory>

For example, for the productCategory-genericName DCinemaLens, I need to be able to grab the parent as DigitalCinema, and similarly for the individual productModel nodes, where the parent would be DCinemaProj.

I’ve tried various different queries in xpath using ancestor, previous-sibling and parent and I still can’t see to grab the node I need.

Here is my code as it stands from giving up on my attempts a few minutes ago.

if ($xml->xpath('//productCategories')) {

    foreach($xml->xpath('//genericName | //productModel') as $genericName){

    echo "<p align='center'>$genericName";

    $type = $genericName->getName();

    echo " - (" . $type . ") ";

    $derp = $xml->xpath("ancestor::productCategory[1]/genericName");

    echo $derp;

    echo '</p>';

    }

    }

I’ve also had some success getting information in an array, but it always just returns every value in the XML again.

$key = 'genericName';

    $derpgleep = $derp[$key];

    echo 'Derp= ' . $derpgleep;

    print_r($derp);

Hopefully there is a really easy solution I am overlooking. I hope I have been clear.

  • 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-25T10:44:15+00:00Added an answer on May 25, 2026 at 10:44 am

    The XPath expression you are using:

     ancestor::productCategory[1]/genericName
    

    would work if you were able to perform that expression starting from the current node. this is not possible indeed starting from the current Array $genericName as it does not contain parents, nor ancestors.

    I think that your option is to re-traverse all the XML tree. This is a sample test which works as required, based on your input sample.

    <?php
    $xml = simplexml_load_file("test_input1.xml");
    
    if ($xml->xpath('//productCategories')) {
    
        foreach($xml->xpath('//genericName') as $genericName){
    
            echo "<p align='center'>$genericName";
    
            $type = $genericName->getName();
    
            echo " - (" . $type . ") ";
    
            $derp = $xml->xpath("//genericName[.='" . 
                $genericName[0] . 
                "']/ancestor::productCategory[2]/genericName");
    
            echo $derp[0];  echo "</p>\n";
        }
    }
    ?>
    

    This will print out the following HTML fragment:

    <p align='center'>DigitalCinema - (genericName) </p>
    <p align='center'>DCinemaProj - (genericName) DigitalCinema</p>
    <p align='center'>DCinemaLens - (genericName) DigitalCinema</p>
    

    While to get the “parent” of productModel you need an xpath like:

            $derp = $xml->xpath("//productModel[.='" . 
                $productModel[0] . 
                "']/parent::productCategory[1]/genericName");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If this has been asked before please forgive me, I could not find it.
Please forgive if this question has been asked numerous times. I recently installed Eclipse
Please forgive me if this question has been asked and answered already. If so,
Bit of a strange one this. Please forgive the semi-pseudo code below. I have
Please forgive my ignorance. Traditionally I've been doing something like this, but it feels
I am new to django so please forgive me if this turns out to
First, please forgive me asking the following as I'm sure it's been asked in
Please forgive the verbosity of the following code example. Using Delphi 2009, I created
Please forgive me if this is a silly question. We're running into a problem
Please forgive me if this is an obvious one. I have an unknown amount

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.