1

I have a question regarding the processing of an XML file using DomDocument in PHP.

<managedObject class="class1" version="version1" distName="distName1" id="id1">
    <p name="a">Data I have</p>
    <p name="b">Some data</p>
    <p name="c">Some data</p>
    <p name="d">Data I need</p>
    <p name="e">Some data</p>
</managedObject>

<managedObject class="class2" version="version2" distName="distName2" id="id2">
    <p name="a">Some data</p>
    <p name="b">Some data</p>
    <p name="c">Some data</p>
    <p name="d">Some data</p>
    <p name="e">Some data</p>
</managedObject>

By only knowing the "a" element, could I get the "d" element, but only if the parent have a certain class and version?

So far I've had a few tries, most recent being:

$nodes = $finder->query("//*[contains(@class, 'class1')][contains(@version, 'version1')]/*[contains(@name, 'a')]");
foreach ($nodes as $node) {
            $Array[$i] = $node;
        }

I used this to see at least some properties of the node but it doesn't return anything.

Any help would be appreciated.

2 Answers2

0

If you can work from Data I have and work back, an Xpath of //p[@name="d" and ../p[@name="a"]/text() = "Data I have" and parent::*[@version="version1"]] should be able to get you what you need

otherwise a sibling and parent check can be done with //p[@name="d" and ../p[@name="a"] and parent::*[@version="version1"]]

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
0

You can use following xpath to locate <p name="d">Data I need</p>

//managedobject[@class='class1'][@version='version1']/p[@name='a']/following-sibling::p[@name='d']

//managedobject[@class='class1'][@version='version1']/p[@name='a'] will specify your parent element with specific class and version and a element

And /following-sibling::p[@name='d'] will locate your d element which is sibling of a

NarendraR
  • 7,577
  • 10
  • 44
  • 82