0

I am trying to get information from an XML file using xpath function. For some reason, it keeps returning an empty array.

XML File

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
 <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
 <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
 <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
 <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
 <Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image1.png"/>
 <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://test.com/" TargetMode="External"/>
 <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
 <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://youtu.be/" TargetMode="External"/>
 <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://youtu.be/" TargetMode="External"/>
 <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="https://youtu.be/" TargetMode="External"/>
</Relationships>

Doing a simple

 $relationships=simplexml_load_file('document.xml.rels');
 print_r($relationships);
 // prints full xml perfectly

 print_r($relationships->xpath('//Relationship'));
 // prints empty array: Array()

Why is $relationships->xpath('//Relationship') printing an empty array?

My goal is to select the relationship by Id using ->xpath('//Relationship[@Id="rId7"]') for example but ->xpath('//Relationship') is not even working?

Note: I've also tried using var_dump() with no success.

Maciek Semik
  • 1,872
  • 23
  • 43

1 Answers1

2

Since you have a namespace xmlns="http://schemas.openxmlformats.org/package/2006/relationships" in your XML file you need to register it using SimpleXMLElement::registerXPathNamespace() before running the query.

Updated code

$relationships=simplexml_load_file('document.xml.rels');
print_r($relationships);
// prints full xml perfectly

foreach($relationships->getDocNamespaces() as $strPrefix => $strNamespace) {
    if(strlen($strPrefix)==0) {
        $strPrefix="a"; //Assign an arbitrary namespace prefix.
    }
    $relationships->registerXPathNamespace($strPrefix,$strNamespace);
}

echo "<br><br>PRINTING<br><br>";

print_r($relationships->xpath('//a:Relationships'));

This will print the Relationships content.

Sabin Chacko
  • 713
  • 6
  • 17